简体   繁体   English

如何从数据库vb.net向datagridviewcombobox添加项目

[英]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 嗨,我有dgv1和dgv2,在cellMouseClickdgv2后,它按选定的行填充DGV1,如何将项目从数据库添加到dgv1中的DataGridViewComboBox,我尝试执行此代码,但没有任何反应

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)

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 . 您可以像对待常规ComboBox控件一样处理DataGridViewComboBoxColumnDataGridViewComboBoxCellDataGridViewComboBoxEditingControl ,这意味着您既可以直接Add到其Items集合,也可以通过DataSource属性绑定数据并设置DisplayMemberValueMember 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. 如果在列上设置属性,则它们将自动传播到该列中的单元格;如果将它们设置在单元格上,则它们将传播到该单元格中的编辑控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM