1 Previous Next 

DataGridView: A DataGridViewCombobox which value member is showed in a textbox after committing.


DataGridView cells have no events beside disposing. Therfore changed values are only committed after a row change or endedit in the underlying collection (and therefore in the grid)
Here a small sample how to do that, it is a Windows Forms Project with only a DataGridView dragged on the surface of the form.
Be aware the two values are bound to each other, so we cannot for instance add a member Mike Feng from Sjanghai in this sample
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object,
                           ByVal e As System.EventArgsHandles MyBase.Load
        Dim DGVCombo As New DataGridViewComboBoxColumn
        Dim TextBox1 As New DataGridViewTextBoxColumn
        DataGridView1.AutoGenerateColumns = False
        Dim dt As New DataTable("Names")
        dt.Columns.Add("State")
        dt.Columns.Add("Name")
        dt.LoadDataRow(New Object() {"Florida""Ken Tucker"}, True)
        dt.LoadDataRow(New Object() {"Netherlands""Cor Ligthert"}, True)
        dt.LoadDataRow(New Object() {"England""John Antonny Oliver"}, True)
        dt.LoadDataRow(New Object() {"Germany""Armin Zingler"}, True)
        dt.LoadDataRow(New Object() {"Illinois""Paul Clement"}, True)
        dt.LoadDataRow(New Object() {"SjangHai""Liliane Teng"}, True)
        DataGridView1.DataSource = dt
        DataGridView1.AllowUserToAddRows = False
        Dim dtCombo = dt.Copy
        dtCombo.TableName = "States"
        DGVCombo.DataSource = dtCombo
        DGVCombo.DisplayMember = "Name"
        DGVCombo.ValueMember = "State"
        DGVCombo.DataPropertyName = "State"
        TextBox1.DataPropertyName = "State"
        DataGridView1.Columns.Add(DGVCombo)
        DataGridView1.Columns.Add(TextBox1)
    End Sub
End Class



1 Previous Next