当前位置:网站首页>C# 如何在dataGridView里设置两个列comboboxcolumn绑定级联事件的一个二级联动效果
C# 如何在dataGridView里设置两个列comboboxcolumn绑定级联事件的一个二级联动效果
2022-07-06 13:27:00 【ac.char】
当改变了公司后,部门那一列的选项也跟真改变。
这个dataGridView 是绑定的userBindingSource
公司这列的DataGridViewComboBoxColumn 是绑定的CoBindingSource
部门这列的DataGridViewComboBoxColumn 绑定的是deptBindingSource
上面这些都是用vs2005自动生成的代码做的。
然后给dataGridView 添加了个监听。当公司这列的某个单元格的值发生改变时触发一个事件。
事件代码如下:
try
{
Point a = dataGridView1.CurrentCellAddress;
int values = (int)this.dataGridView1.Rows[a.X].Cells[1].Value;
this.deptTableAdapter.FillBy(this.dataSet.dept, values);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
给公司这一列的ComboBox添加事件
dataGridView.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView_EditingControlShowing);
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView.CurrentCell.OwningColumn.Name.Equals("公司") && e.Control is ComboBox)
{
(e.Control as ComboBox).SelectedValueChanged -= new EventHandler(ComboBox_SelectedValueChanged);
(e.Control as ComboBox).SelectedValueChanged += new EventHandler(ComboBox_SelectedValueChanged);
}
}
void ComboBox_SelectedValueChanged(object sender, EventArgs e)
{
if ((sender as ComboBox).SelectedItem != null)
{
dataGridView.CurrentRow.Cells["部门"].Value = ((sender as ComboBox).SelectedItem as DataRowView)["部门"];
}
}

// 构建表格实际数据源
DataTable dt = new DataTable();
dt.Columns.Add("c_ID");
dt.Columns.Add("d_ID");
for (int i = 0; i < 10; i += 2)
dt.Rows.Add(new object[] {
i, i % 4 });
dataGridView1.DataSource = dt;
//先构建第一个联动数据列
DataTable dt1 = new DataTable();
dt1.Columns.Add("c_ID");
dt1.Columns.Add("c_name");
for (int i = 0; i < 10; i++)
dt1.Rows.Add(new object[] {
i, "公司" + i.ToString() });
//建立下拉列
DataGridViewComboBoxColumn cc = new DataGridViewComboBoxColumn();
cc.DataSource = dt1;
cc.HeaderText = "公司名称";
cc.DisplayMember = "c_name";
cc.ValueMember = "c_ID";
cc.DataPropertyName = "c_ID";
dataGridView1.Columns.Add ( cc);
//构建第二个级联动数据列
DataTable dt2 = new DataTable();
dt2.Columns.Add("d_ID");
dt2.Columns.Add("d_name");
for (int i = 0; i < 4; i++)
dt2.Rows.Add(new object[] {
i, "部门" + i.ToString() });
//建立下拉列
DataGridViewComboBoxColumn c2 = new DataGridViewComboBoxColumn();
c2.DataSource = dt2;
c2.HeaderText = "部门";
c2.DisplayMember = "d_name";
c2.ValueMember = "d_ID";
c2.DataPropertyName = "d_ID";
dataGridView1.Columns.Add( c2);
//最后隐藏ID列
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].Visible = false;
边栏推荐
- 2017 8th Blue Bridge Cup group a provincial tournament
- MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
- 038. (2.7) less anxiety
- Fzu 1686 dragon mystery repeated coverage
- Start the embedded room: system startup with limited resources
- WEB功能测试说明
- [go][reprint]vscode run a HelloWorld example after configuring go
- Caching strategies overview
- Proxy and reverse proxy
- [MySQL] trigger
猜你喜欢

It's not my boast. You haven't used this fairy idea plug-in!

2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history

嵌入式开发的7大原罪

Internet News: Geely officially acquired Meizu; Intensive insulin purchase was fully implemented in 31 provinces

互联网快讯:吉利正式收购魅族;胰岛素集采在31省全面落地

Quick news: the flybook players' conference is held online; Wechat payment launched "education and training service toolbox"

Fastjson parses JSON strings (deserialized to list, map)

Z function (extended KMP)

Shake Sound poussera l'application indépendante de plantation d'herbe "louable", les octets ne peuvent pas oublier le petit livre rouge?

Is it profitable to host an Olympic Games?
随机推荐
Why do job hopping take more than promotion?
WEB功能测试说明
20220211 failure - maximum amount of data supported by mongodb
document.write()的用法-写入文本——修改样式、位置控制
How do I remove duplicates from the list- How to remove duplicates from a list?
El table table - get the row and column you click & the sort of El table and sort change, El table column and sort method & clear sort clearsort
ACdreamoj1110(多重背包)
首批入选!腾讯安全天御风控获信通院业务安全能力认证
Hill | insert sort
【Redis设计与实现】第一部分 :Redis数据结构和对象 总结
【力扣刷题】一维动态规划记录(53零钱兑换、300最长递增子序列、53最大子数组和)
中国白酒的5场大战
Start the embedded room: system startup with limited resources
Caching strategies overview
2017 8th Blue Bridge Cup group a provincial tournament
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
Fzu 1686 dragon mystery repeated coverage
Fastjson parses JSON strings (deserialized to list, map)
爬虫实战(五):爬豆瓣top250
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software