当前位置:网站首页>C how to set two columns comboboxcolumn in DataGridView to bind a secondary linkage effect of cascading events
C how to set two columns comboboxcolumn in DataGridView to bind a secondary linkage effect of cascading events
2022-07-06 21:39:00 【ac.char】
After changing the company , The options in the Department column have also changed .
This dataGridView
Is bound. userBindingSource
The company column DataGridViewComboBoxColumn
Is bound. CoBindingSource
Department column DataGridViewComboBoxColumn
The binding is deptBindingSource
These are all used vs2005 The automatically generated code does .
And then to dataGridView Added a listener . An event is triggered when the value of a cell in the company column changes .
The event code is as follows :
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);
}
For the company ComboBox
Add event
dataGridView.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView_EditingControlShowing);
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView.CurrentCell.OwningColumn.Name.Equals(" company ") && 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[" department "].Value = ((sender as ComboBox).SelectedItem as DataRowView)[" department "];
}
}
// Build the actual data source of the table
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;
// First, build the first linkage data column
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, " company " + i.ToString() });
// Create a drop-down column
DataGridViewComboBoxColumn cc = new DataGridViewComboBoxColumn();
cc.DataSource = dt1;
cc.HeaderText = " Corporate name ";
cc.DisplayMember = "c_name";
cc.ValueMember = "c_ID";
cc.DataPropertyName = "c_ID";
dataGridView1.Columns.Add ( cc);
// Build the second level linkage data column
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, " department " + i.ToString() });
// Create a drop-down column
DataGridViewComboBoxColumn c2 = new DataGridViewComboBoxColumn();
c2.DataSource = dt2;
c2.HeaderText = " department ";
c2.DisplayMember = "d_name";
c2.ValueMember = "d_ID";
c2.DataPropertyName = "d_ID";
dataGridView1.Columns.Add( c2);
// Last hidden ID Column
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].Visible = false;
边栏推荐
- 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
- Redistemplate common collection instructions opsforset (V)
- ViT论文详解
- b站视频链接快速获取
- 基于InsightFace的高精度人脸识别,可直接对标虹软
- Hill | insert sort
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- [Li Kou brush questions] 32 Longest valid bracket
- JS学习笔记-OO创建怀疑的对象
- 967- letter combination of telephone number
猜你喜欢
【Redis设计与实现】第一部分 :Redis数据结构和对象 总结
Sequoia China, just raised $9billion
for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
Is it profitable to host an Olympic Games?
MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
Five wars of Chinese Baijiu
Caching strategies overview
快讯:飞书玩家大会线上举行;微信支付推出“教培服务工具箱”
愛可可AI前沿推介(7.6)
随机推荐
Guava: use of multiset
numpy 下载安装
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
技术分享 | 抓包分析 TCP 协议
KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
Thinking about agile development
Nodejs tutorial let's create your first expressjs application with typescript
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
npm run dev启动项目报错 document is not defined
50个常用的Numpy函数解释,参数和使用示例
Nodejs教程之Expressjs一篇文章快速入门
Redistemplate common collection instructions opsforlist (III)
Hill | insert sort
JS according to the Chinese Alphabet (province) or according to the English alphabet - Za sort &az sort
el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
Web开发小妙招:巧用ThreadLocal规避层层传值
OneNote in-depth evaluation: using resources, plug-ins, templates
抖音將推獨立種草App“可頌”,字節忘不掉小紅書?
3D人脸重建:从基础知识到识别/重建方法!
C# 如何在dataGridView里设置两个列comboboxcolumn绑定级联事件的一个二级联动效果