当前位置:网站首页>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;
边栏推荐
- C语言:#if、#def和#ifndef综合应用
- Divide candy
- Redistemplate common collection instructions opsforlist (III)
- 字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
- 分糖果
- 袁小林:安全不只是标准,更是沃尔沃不变的信仰和追求
- el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
- 039. (2.8) thoughts in the ward
- 强化学习-学习笔记5 | AlphaGo
- FZU 1686 龙之谜 重复覆盖
猜你喜欢
jvm:大对象在老年代的分配
Quick news: the flybook players' conference is held online; Wechat payment launched "education and training service toolbox"
50 commonly used numpy function explanations, parameters and usage examples
for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
[interpretation of the paper] machine learning technology for Cataract Classification / classification
嵌入式开发的7大原罪
抖音將推獨立種草App“可頌”,字節忘不掉小紅書?
Caching strategies overview
PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
对话阿里巴巴副总裁贾扬清:追求大模型,并不是一件坏事
随机推荐
npm run dev启动项目报错 document is not defined
SQL:存储过程和触发器~笔记
Proxy and reverse proxy
ACdreamoj1110(多重背包)
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
Microsoft technology empowerment position - February course Preview
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Michael smashed the minority milk sign
Set up a time server
Is this the feeling of being spoiled by bytes?
The underlying implementation of string
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
R language for text mining Part4 text classification
Aiko ai Frontier promotion (7.6)
缓存更新策略概览(Caching Strategies Overview)
Aike AI frontier promotion (7.6)
Torch Cookbook
FZU 1686 龙之谜 重复覆盖
1292_FreeROS中vTaskResume()以及xTaskResumeFromISR()的实现分析
3D人脸重建:从基础知识到识别/重建方法!