通过datagridviewcomboboxcolumn接受来自用户的输入

时间:2016-12-20 08:28:19

标签: c# vb.net winforms datagridview windows-forms-designer

我有一个带有 datagridviewcomboboxcolumn DataGridView ,我希望用户从datagridviewcomboboxcolumn中选择一个项目,并且可以选择自己编写,输入字段也应该如果此项目在此Dropdown中不存在,则为Combobox,书面选项将仅插入未添加为下拉列表中的另一个选项的数据库中。

1 个答案:

答案 0 :(得分:0)

要在组合框类型的datagridview中创建一列,您可以使用以下代码:

  1. 在datagridview中添加列

    using System.Data.SqlServerCe;
    
    string sqlConnection = "Data Source";
    SqlCeConnection conn = new SqlCeConnection(sqlConnection);
    //Get bind from database.
    string qryGetCategory = "Query to get data for combo box";
    SqlCeCommand cmdCat = new SqlCeCommand(qryGetCategory, conn);
    SqlCeDataAdapter daCat = new SqlCeDataAdapter(qryGetCategory, conn);
    DataTable dtCat = new DataTable();
    daCat.Fill(dtCat);
    
    //Combobox column.
    DataGridViewComboBoxColumn ComboBoxCol = new DataGridViewComboBoxColumn();
    ComboBoxCol.DataSource = dtCat;
    ComboBoxCol.Name = "Column name";
    ComboBoxCol.ValueMember = "Value of member";
    ComboBoxCol.DisplayMember = "Member to be show";
    ComboBoxCol.DropDownStyle = ComboBoxStyle.DropDown;
    datagridview.Columns.Add(ComboBoxCol);