当前位置:网站首页>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;
边栏推荐
- It's almost the new year, and my heart is lazy
- Z function (extended KMP)
- 50 commonly used numpy function explanations, parameters and usage examples
- VIM basic configuration and frequently used commands
- 红杉中国,刚刚募资90亿美元
- Comparison between multithreaded CAS and synchronized
- [in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- 968 edit distance
- string的底层实现
猜你喜欢
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
抖音將推獨立種草App“可頌”,字節忘不掉小紅書?
50 commonly used numpy function explanations, parameters and usage examples
Study notes of grain Mall - phase I: Project Introduction
[Li Kou brushing questions] one dimensional dynamic planning record (53 change exchanges, 300 longest increasing subsequence, 53 largest subarray and)
Quick news: the flybook players' conference is held online; Wechat payment launched "education and training service toolbox"
抖音将推独立种草App“可颂”,字节忘不掉小红书?
OneNote in-depth evaluation: using resources, plug-ins, templates
jvm:大对象在老年代的分配
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
随机推荐
[go][转载]vscode配置完go跑个helloworld例子
嵌入式开发的7大原罪
@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
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
在最长的距离二叉树结点
C language char, wchar_ t, char16_ t, char32_ Relationship between T and character set
@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
抖音將推獨立種草App“可頌”,字節忘不掉小紅書?
袁小林:安全不只是标准,更是沃尔沃不变的信仰和追求
js中,字符串和数组互转(二)——数组转为字符串的方法
Binary tree node at the longest distance
Nodejs教程之让我们用 typescript 创建你的第一个 expressjs 应用程序
966 minimum path sum
Guava: use of multiset
【论文解读】用于白内障分级/分类的机器学习技术
guava: Multiset的使用
Why do job hopping take more than promotion?
One line by line explanation of the source code of anchor free series network yolox (a total of ten articles, you can change the network at will after reading it, if you won't complain to me)
Hill | insert sort
Study notes of grain Mall - phase I: Project Introduction