简体   繁体   English

将DataGridViewColumn更改为绑定DataGridView中的DataGridViewComboBoxColumn

[英]Change DataGridViewColumn to DataGridViewComboBoxColumn in bound DataGridView

I have a DataGridView bound to a DataTable and I use a SQLiteDataAdapter to update one SQLite database. 我有一个绑定到DataTable的DataGridView,我使用SQLiteDataAdapter来更新一个SQLite数据库。 I'm trying to make one of the columns a DataGridViewComboBoxColumn, but despite I can put items on it and change its cell values I'm no longer able to update it trough the DataAdapter. 我正在尝试将其中一列作为DataGridViewComboBoxColumn,但尽管我可以在其上放置项目并更改其单元格值,但我无法再通过DataAdapter更新它。

I'm doing this once the DataGridView is loaded: 加载DataGridView后我正在这样做:

DataGridViewComboBoxColumn cbCol = new DataGridViewComboBoxColumn();
dataGridView1.Columns.Insert(2, cbCol);
dataGridView1.Columns[2].HeaderText = dataGridView1.Columns[3].HeaderText;
dataGridView1.Columns[3].Visible = false;

string[] cbList = new[] {"item1","item2"};

foreach (string str in cbList)
{
    cbCol.Items.Add(str);
}

for (int i = 0 ; i<= dataGridView1.Rows.Count - 1; i++)
{
    dataGridView1.Rows[i].Cells[2].Value = dataGridView1.Rows[i].Cells[3].Value;
}

The problem is that the ComboBoxColumn is not bound to the DataTable and I have to check for changes and modify the hidden column before running the DataAdapter.Update. 问题是ComboBoxColumn没有绑定到DataTable,我必须在运行DataAdapter.Update之前检查更改并修改隐藏列。

Is there any way to avoid this? 有什么方法可以避免这种情况吗? Perfect scenario would be something like this: 完美的场景将是这样的:

    private void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
    {
       if (columnadded.headertext == "xxx")
       {
           //Then this is column is the same but in a ComboBox format.
           //The ComboBoxColumn, when added, must show the same values and have 
           //the same binding as if if was the regular column.
       }
    }

Create the DataGridViewComboBoxColum , add it to the grid and bind the data. 创建DataGridViewComboBoxColum ,将其添加到网格并绑定数据。 If you set the DataPropertyName property of your column to the db column id then when your data is bound, that data column will get bound to your grid column. 如果将列的DataPropertyName属性设置为db列id,则在绑定数据时,该数据列将绑定到网格列。 The DataGridView will generate other columns automatically (if you set AutoGenerateColumns=true ). DataGridView将自动生成其他列(如果设置AutoGenerateColumns=true )。 Bind a second datasource to the comboboxcolumn itself. 将第二个数据源绑定到组合框本身。

DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn ();
col1.Name = //name of the column
col1.HeaderText = //header text of the column
col1.DataPropertyName = //db column id
col1.DataSource = // datasource for combo
dataGridView1.Columns.Add(col1);

dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = // your datasource

i had just now similar problem, i had to add 我刚才有类似的问题,我不得不补充一下

col1.DisplayMember = "number"; //column name in source table for col1

to above solution and then it worked. 以上解决方案,然后它工作。

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

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