[英]BindingSource with DataGridView Combo Box
我知道您可以將BindingSource
對象與DataGridView一起使用。
是否有可能在其中一列中有一個組合框,仍然可以利用BindingSource
?
是的,它是 - 在C#中查看帶有DataGridView的ComboBox :
將ComboBox與DataGridView一起使用並不復雜,但在進行一些數據驅動的軟件開發時幾乎是必須的。
我已經像這樣創建了一個DataGridView。 現在,我想在DataGridView中顯示“Month”和“Item”而不是“MonthID”和“ItemID”。
本文所描述的基本上是將組合框與單獨的綁定源綁定 - 在本例中為驗證表,其中存儲MonthID
和MonthName
,並根據原始數據中的id顯示月份名稱。
在這里,他設置Month數據源,從月份表中選擇,然后從返回的數據創建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
到原始數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.