shivam mehta shivam mehta - 1 month ago 10
Vb.net Question

How to add dynamic Data in to the datagridviewcombobox column?

I am Developing VB.net application.
In that application I take

DataGirdView
to display data.
I took
DataSource
property of the datagridview to display 3 columns' data directly from database.

After those columns I add another
DataGridViewComboBoxColumn
. Then I a want to add dynamic the data into that
ComboBoxColumn
.
how to do this?

Click here to see image

Private Function CreatComboBoxWithEnum() As DataGridViewComboBoxColumn
Dim combo As New DataGridViewComboBoxColumn()
Sqlconn = New SqlConnection
Sqlconn.ConnectionString = "server=.\SQLEXPRESS_2005;Initial Catalog=MachineShopScheduling ;Integrated Security=SSPI;"
Dim adpter As New SqlDataAdapter
Dim ds As New DataTable
Try
Sqlconn.Open()
Dim Query As String
For Each dr As DataGridViewRow In DataGridView1.Rows
Dim val As String = dr.Cells("SrDataGridViewTextBoxColumn").Value.ToString
Query = "select OperationNo from RoutingCalculation where Sr ='" & val & "' "
COMMAND = New SqlCommand(Query, Sqlconn)
adpter.SelectCommand = COMMAND
adpter.Fill(ds)
combo.DataSource = ds
combo.DataPropertyName = "OperationNo"
combo.Name = "OperationNo"
OperationNo.ValueMember = "OperationNo"
OperationNo.DisplayMember = "OperationNo"
Next

Sqlconn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return combo
End Function

Answer
 Private Sub load_operationNo()
    Sqlconn = New SqlConnection
    Sqlconn.ConnectionString = "server=.\SQLEXPRESS_2005;Initial Catalog=MachineShopScheduling ;Integrated Security=SSPI;"
    Try
        op.Name = "OperationNo"
        DataGridView1.Columns.Add(op)
        Sqlconn.Open()
        Dim Query As String
        Dim i As Integer = 0
        For Each dr As DataGridViewRow In DataGridView1.Rows
            Dim OPno As New DataGridViewComboBoxCell
            With OPno
                Dim adpter As New SqlDataAdapter
                Dim dt As New DataTable
                Dim val As String = dr.Cells("SrDataGridViewTextBoxColumn").Value.ToString
                Query = "select OperationNo from RoutingCalculation where Sr = " & val & " order by sr"
                Using cmd As New SqlCommand(Query, Sqlconn)
                    adpter.SelectCommand = cmd
                    adpter.Fill(dt)
                End Using
                .DataSource = dt
                .ValueMember = "OperationNo"
                .DisplayMember = "OperationNo"
                DataGridView1.Rows(i).Cells("OperationNo") = OPno
                i = i + 1
            End With
            Next
        Sqlconn.Close()
    Catch ex As Exception
        'MessageBox.Show(ex.Message)
    End Try
End Sub

This is the code to add dynamically add the data into DatagridViewComboboxColumn