BindingSource的DataGridView的与组合框 [英] BindingSource with DataGridView Combo Box

查看:147
本文介绍了BindingSource的DataGridView的与组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以使用的BindingSource 对象有一个DataGridView。



是否有可能有一个组合在一列,仍然盒利用的BindingSource


解决方案
<的p>是的,它是 - 看看的组合框的DataGridView在C#




使用的组合框的DataGridView不是那么复杂了,但它几乎是强制性的,而这样做驱动的软件开发的一些数据。





我创建像这样的一个DataGridView。现在,我想在DataGridView中显示月和项,而不是MonthID和项目编号。




从本质上讲是什么文章描述的是一个单独的绑定源绑定的组合框 - 在这种情况下,验证表,其中 MonthID MONTHNAME 存储,和月份名称是基于从原始数据的ID显示的



下面他建立本月数据源,从一个月表中选择,并且然后创建一个的BindingSource 从返回的数据。

  //月数据来源
串selectQueryStringMonth =SELECT MonthID,MonthText FROM Table_Month
SqlDataAdapter的sqlDataAdapterMonth =新SqlDataAdapter的(selectQueryStringMonth,SqlConnection的);
SqlCommandBuilder sqlCommandBuilderMonth =新SqlCommandBuilder(sqlDataAdapterMonth);
的DataTable dataTableMonth =新的DataTable();
sqlDataAdapterMonth.Fill(dataTableMonth);
的BindingSource bindingSourceMonth =新的BindingSource();
bindingSourceMonth.DataSource = dataTableMonth;

然后他补充道月份ComboBoxColumn到DataGridView,使用数据源的BindingSource 上面创建的:

  //添加月二合一
DataGridViewComboBoxColumn ColumnMonth =新DataGridViewComboBoxColumn();
ColumnMonth.DataPropertyName =MonthID;
ColumnMonth.HeaderText =月;
ColumnMonth.Width = 120;
ColumnMonth.DataSource = bindingSourceMonth;
ColumnMonth.ValueMember =MonthID;
ColumnMonth.DisplayMember =MonthText;
dataGridViewComboTrial.Columns.Add(ColumnMonth);



然后终于,他结合了 DataGridView的到原始数据


I know you can use the BindingSource object with a DataGridView.

Is it possible to have a combo box in one of the columns and still take advantage of the BindingSource?

解决方案

Yes, it is - take a look at ComboBox with DataGridView in C#:

Using ComboBox with DataGridView is not that complex anymore but it’s almost mandatory while doing some data driven software development.

I have created a DataGridView like this one. Now, I want to show "Month" and "Item" instead of "MonthID" and "ItemID" in DataGridView.

Essentially what the article describes is binding the comboboxes with a separate binding source - in this case, a validation table, where MonthID and MonthName are stored, and the month name is displayed based on the id from the original data.

Here he sets up the Month data source, selecting from a month table, and then creates a BindingSource from the returned data.

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

Then he adds the month ComboBoxColumn to the DataGridView, using the DataSource as the BindingSource created above:

//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);

And then finally, he binds the DataGridView to the original data.

这篇关于BindingSource的DataGridView的与组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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