如何从vb.net的DataGridViewComboBoxColumn中的SQL Server数据库获取值

时间:2019-01-17 16:33:09

标签: vb.net datagridview vb.net-2010

在选择datagridviewcomboboxcell时,我想要datagridviewcomboboxcolumn中一个表的数据值和datagridviewtextbox列中第二个表的另一个值

我的代码如下:

 Private Sub Populate()
    Dim dt1 As New DataTable
    Dim dt2 As New DataTable

    con.Open()
    Dim Cmd1 As New SqlCommand("select * from Itemmaster where flag='G'", con)
    dt1.Load(Cmd1.ExecuteReader)
    Cmd1.CommandText = "Select * from Itemdetail"
    dt2.Load(Cmd1.ExecuteReader)


    DataGridView1.AutoGenerateColumns = False
    DataGridView1.DataSource = dt1

    Dim colpgCombo As New DataGridViewComboBoxColumn()
    colpgCombo.DataSource = dt1
    colpgCombo.DisplayMember = "Name"
    colpgCombo.ValueMember = "code"
    colpgCombo.DataPropertyName = "Name"
    colpgCombo.HeaderText = "Product Group"
    DataGridView1.Columns.Add(colpgCombo)

    Dim colpru As New DataGridViewTextBoxColumn()
    colpru.DataPropertyName = "Unit"
    colpru.HeaderText = "Unit"
    DataGridView1.Columns.Add(colpru)

    Dim colHsn As New DataGridViewTextBoxColumn()


    colHsn.DataPropertyName = "Hsncode"
    colHsn.HeaderText = "Hsncode"
    DataGridView1.Columns.Add(colHsn)

    Dim colRate As New DataGridViewTextBoxColumn()
    colRate.DataPropertyName = "Rate"
    colRate.HeaderText = "Rate"
    DataGridView1.Columns.Add(colRate)

End Sub

Private Sub ButtonNewItem_Click(sender As Object, e As EventArgs) Handles ButtonNewItem.Click
    Populate()
End Sub

1 个答案:

答案 0 :(得分:0)

Private Sub Populate()
    Dim dt1 As New DataTable
    Dim dt2 As New DataTable

    con.Open()
    Dim Cmd1 As New SqlCommand("select * from Itemmaster where flag='G'", con)
    dt1.Load(Cmd1.ExecuteReader)
    Cmd1.CommandText = "Select * from Itemdetail"
    dt2.Load(Cmd1.ExecuteReader)


    DataGridView1.AutoGenerateColumns = False
    DataGridView1.DataSource = dt1

    Dim colpgCombo As New DataGridViewComboBoxColumn()
    colpgCombo.DataSource = dt1
    colpgCombo.DisplayMember = "Name"
    colpgCombo.ValueMember = "code"
    colpgCombo.DataPropertyName = "Name"
    colpgCombo.HeaderText = "Product Group"
    DataGridView1.Columns.Add(colpgCombo)

    Dim colpru As New DataGridViewTextBoxColumn()
    colpru.DataPropertyName = "Unit"
    colpru.HeaderText = "Unit"
    DataGridView1.Columns.Add(colpru)

    Dim colHsn As New DataGridViewTextBoxColumn()


    colHsn.DataPropertyName = "Hsncode"
    colHsn.HeaderText = "Hsncode"
    DataGridView1.Columns.Add(colHsn)

    Dim colRate As New DataGridViewTextBoxColumn()
    colRate.DataPropertyName = "Rate"
    colRate.HeaderText = "Rate"
    DataGridView1.Columns.Add(colRate)


End Sub
Private Sub ButtonNewItem_Click(sender As Object, e As EventArgs) Handles ButtonNewItem.Click
    Populate()
End Sub