且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在绑定的DataGridView中将DataGridViewColumn更改为DataGridViewComboBoxColumn

更新时间:2023-02-20 15:26:03

创建 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