当前位置:网站首页>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;
边栏推荐
- Quick access to video links at station B
- Torch Cookbook
- 红杉中国,刚刚募资90亿美元
- JS according to the Chinese Alphabet (province) or according to the English alphabet - Za sort &az sort
- Thinking about agile development
- Divide candy
- 抖音將推獨立種草App“可頌”,字節忘不掉小紅書?
- Description of web function test
- Acdreamoj1110 (multiple backpacks)
- [Li Kou brushing questions] one dimensional dynamic planning record (53 change exchanges, 300 longest increasing subsequence, 53 largest subarray and)
猜你喜欢
Study notes of grain Mall - phase I: Project Introduction
互联网快讯:吉利正式收购魅族;胰岛素集采在31省全面落地
红杉中国,刚刚募资90亿美元
Why do job hopping take more than promotion?
快讯:飞书玩家大会线上举行;微信支付推出“教培服务工具箱”
[redis design and implementation] part I: summary of redis data structure and objects
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
抖音將推獨立種草App“可頌”,字節忘不掉小紅書?
It's not my boast. You haven't used this fairy idea plug-in!
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
随机推荐
js中,字符串和数组互转(二)——数组转为字符串的方法
JS operation DOM element (I) -- six ways to obtain DOM nodes
JPEG2000-Matlab源码实现
50个常用的Numpy函数解释,参数和使用示例
string的底层实现
嵌入式开发的7大原罪
el-table表格——sortable排序 & 出现小数、%时排序错乱
Internet News: Geely officially acquired Meizu; Intensive insulin purchase was fully implemented in 31 provinces
Description of web function test
[sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
【滑动窗口】第九届蓝桥杯省赛B组:日志统计
Redistemplate common collection instructions opsforlist (III)
在Pi和Jetson nano上运行深度网络,程序被Killed
Replace Internet TV set-top box application through digital TV and broadband network
JS learning notes OO create suspicious objects
Five wars of Chinese Baijiu
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中,字符串和数组互转(一)——字符串转为数组的方法
JS学习笔记-OO创建怀疑的对象
js通过数组内容来获取数组下标