Search code examples
vb.netdatagridviewcombobox

how to add items to datagridviewcombobox from database vb.net


hi i have dgv1 and dgv2 after cellMouseClick on dgv2 it fill DGV1 by selected row, how can add items from database to DataGridViewComboBox in dgv1 i try this code but nothing happen

Private Sub DataGridView2_CellMouseClick(sender As Object, e As  DataGridViewCellMouseEventArgs) Handles DataGridView2.CellMouseClick

    Dim indx As Integer = DataGridView2.CurrentRow.Index

Dim y, z, k, v As String

    Dim x, h, n As Long

    Dim t As Integer = 1

    x = DataGridView2(0, indx).Value.ToString()

    y = DataGridView2(1, indx).Value.ToString()

    z = DataGridView2(2, indx).Value.ToString()

    h = DataGridView2(3, indx).Value.ToString()

    n = DataGridView2(4, indx).Value.ToString()

    v = DataGridView2(5, indx).Value.ToString()

    k = DataGridView2(6, indx).Value.ToString()

     Dim ss = CType(DataGridView1.Columns(2), DataGridViewComboBoxColumn)

        ss.DataSource = dataset1

        ss.ValueMember = "UnitNum"

        ss.DisplayMember = "UnitName"

        DataGridView1.Rows.Add(x, y, ss, t, h, h * t, v)

Solution

  • You can treat a DataGridViewComboBoxColumn or a DataGridViewComboBoxCell or a DataGridViewComboBoxEditingControl in the same way as you treat a regular ComboBox control, which means that you can either Add to its Items collection directly or you can bind data via the DataSource property and set the DisplayMember and ValueMember. If you set the properties on the column then they get automatically propagated to cells in that column and if you set them on cell then they get propagated to the editing control in that cell.