在绑定的DataGridView中将DataGridViewColumn更改为DataGridViewComboBoxColumn [英] Change DataGridViewColumn to DataGridViewComboBoxColumn in bound DataGridView

查看:231
本文介绍了在绑定的DataGridView中将DataGridViewColumn更改为DataGridViewComboBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到DataTable的DataGridView,并使用SQLiteDataAdapter更新一个SQLite数据库。我试图将其中一列设为DataGridViewComboBoxColumn,但是尽管我可以在其上放置项目并更改其单元格值,但我无法再通过DataAdapter对其进行更新。

I have a DataGridView bound to a DataTable and I use a SQLiteDataAdapter to update one SQLite database. 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.

一旦加载DataGridView,我就在这样做:

I'm doing this once the DataGridView is loaded:

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;
}

问题是ComboBoxColumn未绑定到DataTable,我必须在运行DataAdapter.Update之前检查更改并修改隐藏的列。

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.

有什么办法可以避免这种情况?完美的场景是这样的:

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.
       }
    }


推荐答案

创建 DataGridViewComboBoxColum ,将其添加到网格并绑定数据。如果将列的 DataPropertyName 属性设置为db列ID,则当绑定数据时,该数据列将被绑定到网格列。 DataGridView将自动生成其他列(如果您设置 AutoGenerateColumns = true )。将第二个数据源绑定到combobox列本身。

Create the DataGridViewComboBoxColum, add it to the grid and bind the data. 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. The DataGridView will generate other columns automatically (if you set 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

这篇关于在绑定的DataGridView中将DataGridViewColumn更改为DataGridViewComboBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆