Event that fires during DataGridViewComboBoxColumn SelectedIndexChanged

后端 未结 3 1201
暗喜
暗喜 2020-12-01 14:17

I have DataGridView with two columns. The first column is TextBoxCol(DataGridViewTextBoxColumn) and the Second one is ComboBoxCol(DataGridVie

相关标签:
3条回答
  • 2020-12-01 14:39

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

    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.


        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

    0 讨论(0)
  • 2020-12-01 14:43

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

    ComboBox comboBox = e.Control as ComboBox;

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

    public class 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;
    }
    
    0 讨论(0)
  • 2020-12-01 14:46

    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.

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

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题