4
我知道你可以使用DataGridView的BindingSource
对象。BindingSource与DataGridView组合框
是否有可能在其中一列有组合框,并仍然利用BindingSource
?
我知道你可以使用DataGridView的BindingSource
对象。BindingSource与DataGridView组合框
是否有可能在其中一列有组合框,并仍然利用BindingSource
?
是的,它是 - 看看ComboBox with DataGridView in C#:
使用带的DataGridView组合框不那么复杂了,但它几乎是强制性的,而这样做驱动的软件开发的一些数据。
我创建了这样一个一个DataGridView。现在,我想在DataGridView中显示“Month”和“Item”而不是“MonthID”和“ItemID”。
本质上讲本文介绍的是一个单独的绑定源绑定的组合框 - 在这种情况下,验证表,其中MonthID
和MonthName
存储和月份名称显示基于从原始数据的ID 。
在这里,他设置月份数据源,从月份表中选择,然后从返回的数据中创建一个BindingSource
。
//Month Data Source
string selectQueryStringMonth = "SELECT MonthID,MonthText FROM Table_Month";
SqlDataAdapter sqlDataAdapterMonth = new SqlDataAdapter(selectQueryStringMonth, sqlConnection);
SqlCommandBuilder sqlCommandBuilderMonth = new SqlCommandBuilder(sqlDataAdapterMonth);
DataTable dataTableMonth= new DataTable();
sqlDataAdapterMonth.Fill(dataTableMonth);
BindingSource bindingSourceMonth = new BindingSource();
bindingSourceMonth.DataSource = dataTableMonth;
接着,他添加了一个月ComboBoxColumn到DataGridView,使用DataSource
如上面创建的BindingSource
:
//Adding Month Combo
DataGridViewComboBoxColumn ColumnMonth = new DataGridViewComboBoxColumn();
ColumnMonth.DataPropertyName = "MonthID";
ColumnMonth.HeaderText = "Month";
ColumnMonth.Width = 120;
ColumnMonth.DataSource = bindingSourceMonth;
ColumnMonth.ValueMember = "MonthID";
ColumnMonth.DisplayMember = "MonthText";
dataGridViewComboTrial.Columns.Add(ColumnMonth);
然后终于,他结合了DataGridView
原始数据。