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