How to unselect a combobox in a datagridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bluberiman
    New Member
    • May 2010
    • 6

    How to unselect a combobox in a datagridview

    I have a couple of combobox in a datagridview control (winform)

    1-I select a new value.
    2-I click elsewhere on the form (out of any controls OR out of any cells but inside the DGV control)

    If I use my mousewheel, the value of the combobox I just edited may be changed. I would like the combobox to lose focus once I edited it.

    I already have an event that is fired when a combobox is changed but after that nothing works... I tried to select another control and to use the ClearSelection method of the DGV but it doesn't work.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I can't figure out how to get the changed event on a combobox in a datagrid view that doesn't require me to actually click off the combobox (if you could post some code that would help) but I did put an event on endedit that made a focus call on a button I stuck somewhere on my form. My datagridview lost focus and the mouse wheel wouldn't scroll the combobox.

    Does that help at all?

    Comment

    • Bluberiman
      New Member
      • May 2010
      • 6

      #3
      Originally posted by GaryTexmo
      I can't figure out how to get the changed event on a combobox in a datagrid view that doesn't require me to actually click off the combobox (if you could post some code that would help) but I did put an event on endedit that made a focus call on a button I stuck somewhere on my form. My datagridview lost focus and the mouse wheel wouldn't scroll the combobox.

      Does that help at all?
      Here's how I did :

      Code:
      Private void datagridview1_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
      {
      Combobox combo=e.control as Combobox;
      combo.SelectedIndexChanged -=new EventHandler(ComboBox_SelectedIndexChanged);
      combo.SelectedIndexChanged +=new EventHandler(ComboBox_SelectedIndexChanged);
      
      }
      
      private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
      {
      //Code here
      }
      I tried what you suggested. I put a textbox.selecte d(); in a cellEndEdit but the event is only fired if the user click on another cell of the DGV, it is not fired if the user click elsewhere(save on another control outside the DGV)

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Well, it worked for me... kind of.

        Code:
                private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                {
                    ComboBox c = e.Control as ComboBox;
                    if (c != null)
                    {
                        c.SelectedIndexChanged -= new EventHandler(CBEvent);
                        c.SelectedIndexChanged += new EventHandler(CBEvent);
                    }
                }
        
                private void CBEvent(object sender, EventArgs e)
                {
                    ComboBox c = sender as ComboBox;
                    if (c != null)
                    {
                        Console.WriteLine(c.SelectedIndex);
                        button1.Focus();
                    }
                }
        Unfortunately, that throws a null exception in Application.Run (*sigh*), so I think it might be a Microsoft bug. I get the exception even if I just do an EndEdit on the DGV so I'm really not sure what's going on with that.

        That said, I did this in the SelectedIndexCh anged event and it gets the behaviour you're looking for...

        Code:
                private void CBEvent(object sender, EventArgs e)
                {
                    ComboBox c = sender as ComboBox;
                    if (c != null)
                    {
                        dataGridView1.Select();
                    }
                }

        Comment

        • Bluberiman
          New Member
          • May 2010
          • 6

          #5
          Hum... I tried so many things that I forgot to try select on the DGV in that event... it works, thanks!

          Comment

          Working...