当前位置:网站首页>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;
边栏推荐
- 技术分享 | 抓包分析 TCP 协议
- 数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
- guava:创建immutableXxx对象的3种方式
- guava:Collections.unmodifiableXXX创建的collection并不immutable
- Microsoft technology empowerment position - February course Preview
- guava: Multiset的使用
- Redistemplate common collection instructions opsforzset (VI)
- Replace Internet TV set-top box application through digital TV and broadband network
- Four common ways and performance comparison of ArrayList de duplication (jmh performance analysis)
- Forward maximum matching method
猜你喜欢
It's not my boast. You haven't used this fairy idea plug-in!
Study notes of grain Mall - phase I: Project Introduction
Why does MySQL index fail? When do I use indexes?
Efficiency tool +wps check box shows the solution to the sun problem
Is it profitable to host an Olympic Games?
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
[redis design and implementation] part I: summary of redis data structure and objects
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
【力扣刷题】32. 最长有效括号
随机推荐
Set up a time server
MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
It's not my boast. You haven't used this fairy idea plug-in!
MySQL - transaction details
[redis design and implementation] part I: summary of redis data structure and objects
Internet News: Geely officially acquired Meizu; Intensive insulin purchase was fully implemented in 31 provinces
b站视频链接快速获取
Binary tree node at the longest distance
Thinking about agile development
Yuan Xiaolin: safety is not only a standard, but also Volvo's unchanging belief and pursuit
1292_FreeROS中vTaskResume()以及xTaskResumeFromISR()的实现分析
039. (2.8) thoughts in the ward
Yyds dry inventory run kubeedge official example_ Counter demo counter
C language: comprehensive application of if, def and ifndef
麦趣尔砸了小众奶招牌
C# 如何在dataGridView里设置两个列comboboxcolumn绑定级联事件的一个二级联动效果
Comparison between multithreaded CAS and synchronized
抖音将推独立种草App“可颂”,字节忘不掉小红书?
Three schemes of SVM to realize multi classification
guava:Collections. The collection created by unmodifiablexxx is not immutable