0

I have a datagridview with 3 columns - Name [string], Width [Integer], Thickness[Integer] . The datasource has width and thickness coming from a foreign tables id [Integer] . I would like to display in the datagridview the actual text values instead of the ID values. I am using a dataset with a standard fillby (select all) . I also have two combobox dropdowns that are to be bound by value to the bindingsource that the datagridview is bound to. How do I have the DataGridView Columns for thickness and width display the Text Value of the Name field coming from the WidthBindingSource and ThicknessBindingSource.

I tried to handle this in the CellFormatting Event of the DataGridView but I can not change the column to use formatted value type of string in the event args e.Value etc..

Code posted below.

Private Sub dgvDimension_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvDimension.CellFormatting
    Dim dgv As DataGridView = DirectCast(sender, DataGridView)

    Dim IsWidthColumn As Boolean = dgv.Columns(e.ColumnIndex).Name.ToLower() = "width"
    Dim IsThicknessColumn As Boolean = dgv.Columns(e.ColumnIndex).Name.ToLower() = "thickness"

    Dim columnName As String = dgv.Columns(e.ColumnIndex).Name


    If IsWidthColumn OrElse IsThicknessColumn Then

        If dgv.Columns(e.ColumnIndex).Name.ToLower() = columnName AndAlso e.RowIndex >= 0 AndAlso TypeOf dgv(columnName, e.RowIndex).Value Is Integer Then

            Dim dt As DataTable = New DataTable

            Select Case columnName.ToLower()

                Case "width"

                    dt = DirectCast(WidthBindingSource1.DataSource, DataTable)

                    Exit Select

                Case "thickness"

                    dt = DirectCast(ThicknessBindingSource.DataSource, DataTable)

                    Exit Select

                Case Else

            End Select



            Dim expression As String = "Id=" & dgv(columnName, e.RowIndex).Value.ToString()

            Dim dRow As DataRow = dt.Select(expression).FirstOrDefault

            If Not dRow Is Nothing Then


                e.Value = dRow("Name").ToString()

                e.FormattingApplied = True


            End If

        End If


    End If


End Sub

More Code info as follows - the structure of the tables, bindingsource, dataset and query info.

Select ID, Name, Width, Thickness From Dimension [DimensionBindingSource DataSet.Dimension]

[Width and Thickness need to appear in DataGridView as Width.Name and Thickness.Name - I need to be able to update the Dimension table from this form using the DimensionBindingSource. ]

Select ID, Name From Width [WidthBindingSource DataSet.Width]

Select ID, Name From Thickness  [ThicknessBindingSource DataSet.Thickness]


My cmbWidth and cmbThickness SelectedValues are bound to Dimension.Width, Dimension.Thickness 
Ken
  • 2,518
  • 2
  • 27
  • 35
  • Here you can find more options: [How to bind a column from second level list on bindsource in winforms datagridview](http://stackoverflow.com/a/36476739/3110834). – Reza Aghaei Aug 16 '16 at 20:46
  • @RezaAghaei still says formatted value of the cell has the wrong type. The binding source for the dgv is using Integers - I am trying to mash up the text values for those integers (from related table) - text and put it in the cell text. Also I am not using a Combobox column - the grid needs to be read only - the combos are in a panel next to the grid . – Ken Aug 16 '16 at 21:05
  • Using `DataGridViewComboBoxColumn` is the most simple option which you have. Just set its `DisplayStyle` to `DataGridViewComboBoxDisplayStyle.Nothing` and it wont show drop down button. Follow the linked post or the link in the linked post :) – Reza Aghaei Aug 16 '16 at 21:12
  • @RezaAghaei ok I will try that - I was reading the post you provided, a lot looks like typed classes and modifying those - I am trying to avoid changing the dataset from defaults as others will be working on the project and they are in the very beginning stages of oop & .NET code writing. – Ken Aug 16 '16 at 21:27
  • Don't change those classes, just read *Using ComboBox Column* in [this post](http://stackoverflow.com/a/36476739/3110834) carefully. Also *Option1* in [this post](http://stackoverflow.com/a/35117378/3110834) is useful. Both of them are talking about setting up a `DataGridViewComboBoxColumn` without need to change in your model classes just using designer. – Reza Aghaei Aug 16 '16 at 21:37
  • why not get the values in your query using joins than you dont need to anything at all in your datagrid to display the values ? – GuidoG Aug 17 '16 at 08:01
  • @GuidoG I thought about this - using the dataset designer is not something I normally do. How will this affect my updates and inserts - if I use a join statement - will the designer automatically create those for me? Because when the dgridview is updated - I am only interested in updating the width, length fields with the integers and not the names . – Ken Aug 17 '16 at 15:33
  • I never ever use the dataset designer it has caused me so many problems in the past so I dont know the answer to that. Sorry to hear that you have to use this thing – GuidoG Aug 17 '16 at 15:42

0 Answers0