事件DataGridViewComboBoxColumn的SelectedIndexChanged期

2019-06-02 16:51发布

我有DataGridView有两列。 第一列是TextBoxCol(DataGridViewTextBoxColumn)第二个是ComboBoxCol(DataGridViewComboBoxColumn)

我怎样才能改变的值TextBoxColComboBoxCol改变其选定的指标值? (当选择指数改变这应该发生ComboBoxCol 。离开后列不一样dataGridView_CellValueChanged

我已阅读几乎是我有,但我不明白的答案(这应该是在复选标记正确基地)同一问题的一个话题。 这里的链接。 -Almost同一主题

Answer 1:

给这两个简单的方法一个去(在顶部方法的“1”是组合框列的索引)

你会让你修改线将是二传手线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();
    }



Answer 2:

这个答案是在几个地方左右浮动。 使用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();
        }
     }


Answer 3:

这种联系是正确的。 处理EditingControlShowing event的DataGridView的。 在此事件处理程序,检查当前列是你的兴趣。 而且,然后创建一个临时的组合框对象: -

ComboBox comboBox = e.Control as ComboBox;

MSDN有一个样本:见例子部分在这里 。 注意 Inheritance HierarchyClass Syntax在MSDN链接: -

公共类DataGridViewComboBoxEditingControl: 组合框 ,IDataGridViewEditingControl

private DataGridView dataGridView1 = new DataGridView();

private void AddColorColumn()
{
    DataGridViewComboBoxColumn comboBoxColumn =
        new DataGridViewComboBoxColumn();
    comboBoxColumn.Items.AddRange(
        Color.Red, Color.Yellow, Color.Green, Color.Blue);
    comboBoxColumn.ValueType = typeof(Color);
    dataGridView1.Columns.Add(comboBoxColumn);
    dataGridView1.EditingControlShowing +=
        new DataGridViewEditingControlShowingEventHandler(
        dataGridView1_EditingControlShowing);
}

private void dataGridView1_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)
{
    ((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
}


文章来源: Event that fires during DataGridViewComboBoxColumn SelectedIndexChanged