当前位置:网站首页>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;
边栏推荐
- 50 commonly used numpy function explanations, parameters and usage examples
- 20220211 failure - maximum amount of data supported by mongodb
- c语言char, wchar_t, char16_t, char32_t和字符集的关系
- 红杉中国,刚刚募资90亿美元
- guava:Collections. The collection created by unmodifiablexxx is not immutable
- Fzu 1686 dragon mystery repeated coverage
- uni-app App端半屏连续扫码
- Nodejs教程之Expressjs一篇文章快速入门
- The difference between break and continue in the for loop -- break completely end the loop & continue terminate this loop
- 【Redis设计与实现】第一部分 :Redis数据结构和对象 总结
猜你喜欢

缓存更新策略概览(Caching Strategies Overview)
![[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs](/img/66/4d94ae24e99599891636013ed734c5.png)
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs

Sequoia China, just raised $9billion

跨分片方案 总结

Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software

Why do job hopping take more than promotion?

Study notes of grain Mall - phase I: Project Introduction

【滑动窗口】第九届蓝桥杯省赛B组:日志统计

Is this the feeling of being spoiled by bytes?

Common English vocabulary that every programmer must master (recommended Collection)
随机推荐
快讯:飞书玩家大会线上举行;微信支付推出“教培服务工具箱”
c语言char, wchar_t, char16_t, char32_t和字符集的关系
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
缓存更新策略概览(Caching Strategies Overview)
document. Usage of write () - write text - modify style and position control
MySQL - transaction details
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
JS operation DOM element (I) -- six ways to obtain DOM nodes
50个常用的Numpy函数解释,参数和使用示例
在最长的距离二叉树结点
R3live notes: image processing section
Redistemplate common collection instructions opsforlist (III)
Acdreamoj1110 (multiple backpacks)
14年本科毕业,转行软件测试,薪资13.5K
Fzu 1686 dragon mystery repeated coverage
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
Technology sharing | packet capturing analysis TCP protocol
3D人脸重建:从基础知识到识别/重建方法!
Fastjson parses JSON strings (deserialized to list, map)
string的底层实现