C# 在 DataGridViewComboBoxColumn SelectedIndexChanged 期间触发的事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11141872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:41:04  来源:igfitidea点击:

Event that fires during DataGridViewComboBoxColumn SelectedIndexChanged

c#.netwinformsdatagridviewdatagridviewcomboboxcell

提问by Kalix Diona

I have DataGridViewwith two columns. The first column is TextBoxCol(DataGridViewTextBoxColumn)and the Second one is ComboBoxCol(DataGridViewComboBoxColumn).

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

How can I change the value of TextBoxColwhen ComboBoxColchanges its selected index value? (This should happen when selected index changed in ComboBoxCol. Not after leaving the column, like dataGridView_CellValueChanged)

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

I have read one topic with almost the same problem that I am having but I dont understand the answer(which should be correct base on the check mark). Here's the link. -Almost same topic

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

采纳答案by KreepN

Give these two simple methods a go (the '1' in the top method is the index of the combobox column)

试试这两个简单的方法(顶部方法中的“1”是组合框列的索引)

The line that you would make you modifications to would be the setter line cel.Value =, as you may change it to whatever you like.

您要修改的行将是 setter line 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();
    }


enter image description here

在此处输入图片说明

回答by Angshuman Agarwal

That link is correct. Handle the EditingControlShowing eventof DataGridView. In this event handler, check if the current column is of your interest. And, then create a temporary combobox object :-

该链接是正确的。处理EditingControlShowing eventDataGridView 的。在此事件处理程序中,检查当前列是否是您感兴趣的。然后创建一个临时组合框对象:-

ComboBox comboBox = e.Control as ComboBox;

ComboBox comboBox = e.Control as ComboBox;

MSDN has a sample: See in the example section here. Note theInheritance Hierarchy& Class Syntaxin the msdn link : -

MSDN 有一个示例:请参阅此处的示例部分。 请注意msdn 链接中Inheritance Hierarchy& Class Syntax:-

public class DataGridViewComboBoxEditingControl : ComboBox, IDataGridViewEditingControl

公共类 DataGridViewComboBoxEditingControl : ComboBox, 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;
}

回答by Severun

This answer is floating around in a couple of places.

这个答案在几个地方浮动。

Using the DataGridViewEditingControlShowingEventHandler will fire more events than you intend. In my testing it fired the event multiple times.

使用 DataGridViewEditingControlShowingEventHandler 将触发比您预期更多的事件。在我的测试中,它多次触发了该事件。

Also using the combo.SelectedIndexChanged -= event will not really remove the event, they just keep stacking.

同样使用 combo.SelectedIndexChanged -= 事件不会真正删除事件,它们只是继续堆叠。

Anyway, I found a solution that seems to work. I'm including a code sample below:

无论如何,我找到了一个似乎有效的解决方案。我在下面包含了一个代码示例:

// 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();
    }
}