c# – 如何从数据集中设置DataGridViewComboBoxColumn值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何从数据集中设置DataGridViewComboBoxColumn值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2984字,纯文字阅读大概需要5分钟。
内容图文
假设我有两张桌子:
work_hours
work_hours_id | date | _project_id
1 1.2. 10
2 1.2. 11
3 1.2. 10
project
project_id | project_name
10 pr1
11 pr2
12 pr3
在DataGridView中我想看到这个:
work_hours_id | date | _person_id | project_name(DataGridViewComboBoxColumn)
1 1.2. 10 pr1
2 1.2. 11 pr2
3 1.2. 10 pr1
我怎么能这样做?
2.如果我使用SqlCommandBuilder将pr1(work_hours_id = 3)更改为pr3(DataGridViewComboBoxColumn),是否可以保存表work_hours中的更改?
string query = "SELECT work_hours.work_hours_id, work_hours.date FROM work_hours
LEFT OUTER JOIN project ON work_hours._project_id = project.project_id ORDER BY work_hours.date;
SELECT * FROM project ORDER BY project_name";
SqlCommand sqlcmd = new SqlCommand(query, conn);
da = new SqlDataAdapter(query, connectionString);
cBuilder = new SqlCommandBuilder(da);
dt = new DataTable();
ds = new DataSet();
da.Fill(dt);
da.Fill(ds);
DataGridViewComboBoxColumn columnCb = new DataGridViewComboBoxColumn();
columnCb.DataPropertyName = "_project_id";
columnCb.DataSource = ds.Tables[1];
columnCb.ValueMember = "project_id";
columnCb.DisplayMember = "project_name";
bSource = new BindingSource();
bSource.DataSource = dt;
dataGridView1.DataSource = bSource;
dataGridView1.Columns.Add(columnCb);
解决方法:
如果我没有误解你想设置datagridview的组合框列的数据源..你可以这样设置:
/// if you use OfType Method then you will get advantage of to use as normal control
/// means this method will give you advantage of can use all method and properties
/// of the control which you want to implement in DataGridView
/// Controls.OfType method will check all the columns and get an array with
/// what you give as search criteria..here, the criteria is ComboboxColumn..
/// depends of your need you can give comboboxcell also..
/// Element at method selects the zero based index in array which filtered by criteria
/// if you use only one of the given type then you can use .First() instead of .ElementAt()
yourDataGridViewName.Controls.OfType<DataGridViewComboBoxColumn>()
.ElementAt(indexNoOfTheDGVComboBox).DataSource = YourDataRetrievingMethod;
//i.e. ds.Tables[indexNoOfTheTable].Columns[IndexOfTheColumn]
/// and you can set DisplayMember and ValueMember as the same way..
/// i give combocell and combocolumn together to show the syntax.
yourDataGridViewName.Controls.OfType<DataGridViewComboBoxCell>()
.ElementAt(0).DisplayMember = "YourDisplay";
yourDataGridViewName.Controls.OfType<DataGridViewComboBoxCell>()
.ElementAt(0).ValueMember = "YourValue";
关于第二个问题..如果你问我,那么我更喜欢:要控制“意外节省”,在行的末尾放置一个buttonCell ..当用户更改某些行时,应单击保存按钮.然后在按钮单击方法可以保存更改为正常的插入或更新方法..
只有2个差异..
1-)你需要使用Cell_Click事件并确保使用OfType()方法而不是普通的Button _Click事件进行按钮检查
2-)取决于项目需求,您需要获取DataGridView的RowIndex和/或Cell Address()才能获取并将值传递给SQLQuery
内容总结
以上是互联网集市为您收集整理的c# – 如何从数据集中设置DataGridViewComboBoxColumn值全部内容,希望文章能够帮你解决c# – 如何从数据集中设置DataGridViewComboBoxColumn值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。