Click here to monitor SSC

DataGridViewComboBox - how to tell when selection changes?

Last post 02-13-2014, 8:20 PM by xiaoyuandlg. 6 replies.
Sort Posts: Previous Next
  •  07-01-2006, 3:23 PM Post number 1039

    DataGridViewComboBox - how to tell when selection changes?

    I'm working with the DataGridView, and one of my columns uses DataGridViewComboBox cells.  What I'd like to do is handle a user changing the selected value in the combo box, and change the value in one of the other cells in the row to correspond with the selection.  Unfortunately, I can't figure out how to accomplish this.  Right now I'm handling the CellEndEdit event, but that only fires if I change the selection then click off of the cell.  Is there some way of doing what I need?  I don't understand why the DataGridViewComboBox does not have an OnSelectionChange event or something similar...


  •  07-12-2006, 7:19 AM Post number 1191 in reply to post number 1039

    • James is not online. Last active: 01-04-2013, 6:04 AM James
    • Top 25 Contributor
    • Joined on 11-14-2005
    • Cambridge, UK
    • Pentium Pro

    Re: DataGridViewComboBox - how to tell when selection changes?

    Hi Adam,

    I dont think there is an easy way to do this (although I have not worked extensively with the DataGridView in 2k5) - one way I can think of you could do this would be to hook yourself up to the DataGridView.EditingControlShowing event, the EventArgs e.Control member can then be cast to a ComboBox control to allow you to hook up the SelectedIndexChanged event to your handler. You can then update your other column when you want to when this event fires.

    Remember to unregister your event handler as well - either next time EditingControlShowing fires or when CellEndEdit fires.

    You might also want to experiment with only hooking yourself up once and not unhooking until you destory the form as I have a suspicion that that same combobox control might be used for editing of all cells (or that is at least the way other grids work such as DevExpress, Synfusion, Infragistics etc).

    Hope this helps,

    - James

  •  07-17-2006, 12:15 AM Post number 1270 in reply to post number 1191

    Re: DataGridViewComboBox - how to tell when selection changes?

    Hi James.I meet I met a same problem.According to what you say,my code is such as:

    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)e.Control).SelectedIndexChanged +=
            new EventHandler(ComboBox_SelectedIndexChanged);
    }

    private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        ((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
    }

    But the problem ("only fires if I change the selection then click off of the cell")still exists´╝îcould you analyze this problem for me?

  •  11-04-2009, 6:08 AM Post number 76000 in reply to post number 1270

    Re: DataGridViewComboBox - how to tell when selection changes?

    you can go for DisplayMemberChanged event
  •  12-09-2009, 8:18 AM Post number 78903 in reply to post number 76000

    Re: DataGridViewComboBox - how to tell when selection changes?



    Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

    Dim editingComboBox As ComboBox = e.Control
    AddHandler editingComboBox.SelectedIndexChanged, AddressOf Me.editingComboBox_SelectedIndexChanged

    End Sub
    Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    -vgtyrd-
    This post is provided "as-is"
    it will work :)
  •  12-09-2009, 8:23 AM Post number 78904 in reply to post number 76000

    Re: DataGridViewComboBox - how to tell when selection changes?

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

    {

    //This is private variable ComBoBox.

    if (combo == null)

    {

    combo = (ComboBox)e.Control;

    combo.SelectedIndexChanged +=new EventHandler(combo_SelectedIndexChanged);

    }

    }

    private void combo_SelectedIndexChanged(object sender, EventArgs e)

    {

    MessageBox.Show("Coming");

    }
    vgtyrd
    it will work
  •  02-13-2014, 8:20 PM Post number 117229 in reply to post number 1039

    Re: DataGridViewComboBox - how to tell when selection changes?

    That seems to be something about the gridview combobox column problem. Probably judge this in a programmatic way. Anyway, you may read something about the gridview selection.
View as RSS news feed in XML