none
Event when DataGrid ComboBox value changed

    Question

  • hi

    I have datagrid with column type of combo box. I wanted to trigger a method when the user change the value of combo box. Now im using

    this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged);

    this is trigger when the user change the value and set the focus to other control. But i wanted a event which trigger at the instance of user change the value.

    Does anyone have solution for this?

    /kevin
    Thursday, August 06, 2009 9:26 AM

Answers

  • http://social.msdn.microsoft.com/forums/en-US/Vsexpressvc/thread/e77ec5ed-7148-4e4e-bc8f-f38d17a8cc4a

    Have a look to this link:
    Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
    • Marked as answer by Jing0Moderator Thursday, August 13, 2009 9:04 PM
    Thursday, August 06, 2009 9:46 AM
  • Hello kevin
    Use the event editingControlShowing of datagridview for combobox selectedIndexChanged event. like ,

    bool eventBinded = false;
    void dataGridView1_EditingControlShowing(object sender,  DataGridViewEditingControlShowingEventArgs e)
            {
                if (this.dataGridView1.CurrentCell.ColumnIndex == /*Index of combobox column*/)           
                {
                  if(!eventBinded)
                    {
                     ComboBox cmb = e.Control as ComboBox;
                     cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
                     eventBinded =true;
                   }
                }
            }
    • Edited by NareshG Thursday, August 06, 2009 11:09 AM formatting changed
    • Marked as answer by Jing0Moderator Thursday, August 13, 2009 9:04 PM
    Thursday, August 06, 2009 11:01 AM
  • Hi,

     

    cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);

    Please remove the event handle before you call it again. This will avoid cmb.SelectedIndexChanged event fires many times.

     

    For CellValueChanged event, you need to check whether this is a comboboxcell. For example:

           void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)

            {

                if (dataGridView1.CurrentCell is DataGridViewComboBoxCell)

                {

                    Console.WriteLine("This is a DataGridViewComboBoxCell");

                   

                }

            }

     

    Here is an example about handle the ComboBoxCell.SelectedIndexChanged event.
    In the EditingControlShowing event, check whether this is a comboboxcell and add the selectedIndexChanged event.


    FAQ: 17. How do I handle the SelectedIndexChanged event?

     

    If I misunderstood you, or you have further questions, please feel free to tell me.

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jing0Moderator Thursday, August 13, 2009 9:05 PM
    Wednesday, August 12, 2009 8:03 AM

All replies

  • http://social.msdn.microsoft.com/forums/en-US/Vsexpressvc/thread/e77ec5ed-7148-4e4e-bc8f-f38d17a8cc4a

    Have a look to this link:
    Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
    • Marked as answer by Jing0Moderator Thursday, August 13, 2009 9:04 PM
    Thursday, August 06, 2009 9:46 AM
  • hi

    thanks for reply. He has used 'CellEndEdit' event. But this event also fired when we get focus out of the combo box. But this is not my requirement.

    /kevin
    Thursday, August 06, 2009 10:47 AM
  • Hello kevin
    Use the event editingControlShowing of datagridview for combobox selectedIndexChanged event. like ,

    bool eventBinded = false;
    void dataGridView1_EditingControlShowing(object sender,  DataGridViewEditingControlShowingEventArgs e)
            {
                if (this.dataGridView1.CurrentCell.ColumnIndex == /*Index of combobox column*/)           
                {
                  if(!eventBinded)
                    {
                     ComboBox cmb = e.Control as ComboBox;
                     cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
                     eventBinded =true;
                   }
                }
            }
    • Edited by NareshG Thursday, August 06, 2009 11:09 AM formatting changed
    • Marked as answer by Jing0Moderator Thursday, August 13, 2009 9:04 PM
    Thursday, August 06, 2009 11:01 AM
  • hi

    its working fine when i have only one record in the datagrid. But i have more than one record(lines) in the datagrid ( each line has one combo box ). Then the 'cmb_SelectedIndexChanged' fires more than one. And i cant predict when this will fire. 

    Please Naresh help on this to solve. Thanks alot 

    /Kevin
    Thursday, August 06, 2009 12:48 PM
  • do you use eventBinded variable, as describe in code example ?
    Thursday, August 06, 2009 12:54 PM
  • hi 

    yes i did use that variable. 

    I have 3 records in the datagrid. When i click on combo box the 'cmb_SelectedIndexChanged' fires 3 or 2 times before it get droped down. After dropped down and when we click on a value method fires once. 

    Hope ull me to solve this. :)

    /kevin
    Friday, August 07, 2009 3:00 AM
  • It should not, debug it and see why the selectedIndexchanged event fires, when you click on combobx. And
    can you please post your code for combobox binding and editingControlshowing.
    Friday, August 07, 2009 5:24 AM
  • hi

    I tried with your code and didnt change anything. And no idea why its triggering when we click on the combo box before it got droped down. Anyway i found a solution. 

    I replace 

    cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);

    with 

    cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted);

    in your code, now its working :). And if we need the selected value from the combo box, use following code.

    String XXX = ((DataGridViewComboBoxEditingControl)sender).EditingControlFormattedValue.ToString();

    Thanks alot Naresh for helping me. Without ur code, i wouldnt find the solution :)

    /kevin
    Friday, August 07, 2009 8:35 AM
  • Hi,

     

    cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);

    Please remove the event handle before you call it again. This will avoid cmb.SelectedIndexChanged event fires many times.

     

    For CellValueChanged event, you need to check whether this is a comboboxcell. For example:

           void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)

            {

                if (dataGridView1.CurrentCell is DataGridViewComboBoxCell)

                {

                    Console.WriteLine("This is a DataGridViewComboBoxCell");

                   

                }

            }

     

    Here is an example about handle the ComboBoxCell.SelectedIndexChanged event.
    In the EditingControlShowing event, check whether this is a comboboxcell and add the selectedIndexChanged event.


    FAQ: 17. How do I handle the SelectedIndexChanged event?

     

    If I misunderstood you, or you have further questions, please feel free to tell me.

     

    Best regards,

    Ling Wang


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jing0Moderator Thursday, August 13, 2009 9:05 PM
    Wednesday, August 12, 2009 8:03 AM
  • its right way to do it!!!!!

    Thanks!!!!!

    Friday, May 04, 2012 9:33 AM