在DataGridViewComboBoxColumn SelectedIndexChanged期间触发的事件

Kal*_*ona 22 .net c# datagridview winforms datagridviewcomboboxcell

我有DataGridView两个专栏.第一列是TextBoxCol(DataGridViewTextBoxColumn)第二列,第二列是ComboBoxCol(DataGridViewComboBoxColumn).

我怎样才能改变的值TextBoxColComboBoxCol改变其选定的指标值?(这应该在选择的索引更改时发生ComboBoxCol.不是在离开列之后,如dataGridView_CellValueChanged)

我已经阅读了一个与我遇到的问题几乎相同的主题,但我不明白答案(根据复选标记应该是正确的).这是链接. - 几乎相同的主题

Sev*_*run 41

这个答案在几个地方浮动.使用DataGridViewEditingControlShowingEventHandler将触发比您想要的更多事件.在我的测试中,它多次触发了该事件.另外使用combo.SelectedIndexChanged - =事件不会真正删除事件,它们只是保持堆叠.无论如何,我找到了一个似乎有效的解决方案.我在下面包含一个代码示例:

// Add the events to listen for
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);



// This event handler manually raises the CellValueChanged event 
// by calling the CommitEdit method. 
void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        // This fires the cell value changed handler below
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    // My combobox column is the second one so I hard coded a 1, flavor to taste
    DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[1];
    if (cb.Value != null)
    {
         // do stuff
         dataGridView1.Invalidate();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,绝对是一个更好的解决方案,选定的解决方案会多次触发事件. (2认同)
  • 很好的答案,恕我直言,应该被接受!只需添加一件事:在 `CellValueChanged` 处理程序中,您可能想检查哪个列/单元格已更改,而不是通常“执行操作”;-) 例如 `if (e.ColumnIndex == dataGridView1.Columns["Column2"].索引)...` (2认同)
  • 在 CurrentCellDirtyStateChanged 事件处理程序中提交更改后,必须调用 DataGridView.BeginEdit(),将“焦点”放回到 EditingControl(ComboBox)上,并真正使其行为类似于 ComboBox 的 SelectedIndexChanged 事件。 (2认同)

Kre*_*epN 31

给出这两个简单的方法(top方法中的'1'是组合框列的索引)

您将修改的行将是setter行cel.Value =,因为您可以将其更改为您喜欢的任何内容.


    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex == 1 && e.Control is ComboBox)
        {
            ComboBox comboBox = e.Control as ComboBox;
            comboBox.SelectedIndexChanged -= LastColumnComboSelectionChanged;
            comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
        }
    }

    private void LastColumnComboSelectionChanged(object sender, EventArgs e)
    {
        var currentcell = dataGridView1.CurrentCellAddress;
        var sendingCB = sender as DataGridViewComboBoxEditingControl;
        DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[0];
        cel.Value = sendingCB.EditingControlFormattedValue.ToString();
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 确保你还使用`comboBox.SelectedIndexChanged - = LastColumnComboSelectionChanged删除任何现有的事件处理程序;` (8认同)