0

我有一个数据网格,其中 DatagridComboboxcolumn 作为 winforms 中的列之一。组合框包含两个项目 Y,N。如果用户选择 Y,我需要更改同一行的两列的值。当用户选择“N”时也会发生同样的事情。

我试图注册 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, 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)
        {

        }
4

1 回答 1

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));
}
于 2012-06-06T16:33:22.263 回答