DataGridViewComboBoxColumn-访问ComboBox附加单击事件 [英] DataGridViewComboBoxColumn - access ComboBox to attach click event
问题描述
我想创建 DataGridViewComboBoxColumn
并附加到它的组合框 Click
事件(我想在其上生成它的数据源
I want to create DataGridViewComboBoxColumn
and attach to it's combo box Click
event (i want to generate it's datasource on click only).
推荐答案
虽然我不知道为什么需要 Click
该 ComboBox
控件的事件,
您可以使用 EditingControlShowing
事件访问该组合框:
While I have no idea about why you need Click
event of that ComboBox
control,
you can access that combo box using EditingControlShowing
event:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//Check if the event is for your column, for example column 1
if (this.dataGridView1.CurrentCell.ColumnIndex == 1)
{
var comboBox = e.Control as DataGridViewComboBoxEditingControl;
comboBox.Click -= comboBox_Click;
comboBox.Click += comboBox_Click;
}
}
private void comboBox_Click(object sender, EventArgs e)
{
var comboBox = sender as DataGridViewComboBoxEditingControl;
//use comboBox here
}
但是您应该知道,可以在datagridview的 CellClick
事件中也为您设置 DataSource
列:
But you should know, you can set the DataSource
for you column in CellClick
event of your datagridview too:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex==1 && e.RowIndex>-1)
{
//Check if the event is for your column, for example column 1
var column = (DataGridViewComboBoxColumn)this.dataGridView1.Columns[e.ColumnIndex];
//use column.DataSource
}
}
您应该知道的另一件事是,如果您设置的数据源不包含此列的一个单元格的值,则在呈现该列时会收到错误。
And another important thing you should know is if you set a datasource that not contains the value of one of cells of this column, you will receive errors while rendering the column.
这篇关于DataGridViewComboBoxColumn-访问ComboBox附加单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!