我有一个DatagridComboboxcolumn的数据网格作为winforms中的一列。 Combobox包含两个项目Y,N。 如果用户选择Y,我需要更改同一行的两列的值。 当用户选择“N”时会发生同样的事情。
我尝试按如下方式注册ComboBox_SelectedIndexChanged。
但是无法获取所选行的行索引或coulmn索引并更改相同行列的值。
请尽快帮助我。
private void gridTesr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if(combo != null)
{
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
combo.SelectedIndexChanged -=new EventHandler(ComboBox_SelectedIndexChanged);
// Add the event handler.
combo.SelectedIndexChanged +=new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
答案 0 :(得分:0)
尝试声明自己的方法来处理ComboBox.SelectedIndexChanged
事件。
private void gridTesr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if(combo != null)
{
// Remove an existing event-handler, if present
combo.SelectedIndexChanged -= this.MyComboEventHandler;
// Add the event handler
combo.SelectedIndexChanged += this.MyComboEventHandler;
}
}
private void MyComboEventHandler(object sender, EventArgs e)
{
string myValue = ((ComboBox)sender).SelectedItem.ToString();
DataGridViewCell cell = gridTesr.CurrentCell;
MessageBox.Show(string.Format("The current ComboBox resides in the Row {0} and Column {1} and it currently has the {3} value.",
cell.RowIndex, cell.ColumnIndex, myValue));
}