By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,264 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,264 IT Pros & Developers. It's quick & easy.

How to unselect a combobox in a datagridview

P: 6
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.
May 28 '10 #1

✓ answered by GaryTexmo

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

Expand|Select|Wrap|Line Numbers
  1.         private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  2.         {
  3.             ComboBox c = e.Control as ComboBox;
  4.             if (c != null)
  5.             {
  6.                 c.SelectedIndexChanged -= new EventHandler(CBEvent);
  7.                 c.SelectedIndexChanged += new EventHandler(CBEvent);
  8.             }
  9.         }
  10.  
  11.         private void CBEvent(object sender, EventArgs e)
  12.         {
  13.             ComboBox c = sender as ComboBox;
  14.             if (c != null)
  15.             {
  16.                 Console.WriteLine(c.SelectedIndex);
  17.                 button1.Focus();
  18.             }
  19.         }
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 SelectedIndexChanged event and it gets the behaviour you're looking for...

Expand|Select|Wrap|Line Numbers
  1.         private void CBEvent(object sender, EventArgs e)
  2.         {
  3.             ComboBox c = sender as ComboBox;
  4.             if (c != null)
  5.             {
  6.                 dataGridView1.Select();
  7.             }
  8.         }

Share this Question
Share on Google+
4 Replies


GaryTexmo
Expert 100+
P: 1,501
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?
May 28 '10 #2

P: 6
@GaryTexmo
Here's how I did :

Expand|Select|Wrap|Line Numbers
  1. Private void datagridview1_EditingControlShowing(object sender,DataGridViewEditingControlShowingEventArgs e)
  2. {
  3. Combobox combo=e.control as Combobox;
  4. combo.SelectedIndexChanged -=new EventHandler(ComboBox_SelectedIndexChanged);
  5. combo.SelectedIndexChanged +=new EventHandler(ComboBox_SelectedIndexChanged);
  6.  
  7. }
  8.  
  9. private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
  10. {
  11. //Code here
  12. }
  13.  
  14.  
I tried what you suggested. I put a textbox.selected(); 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)
May 28 '10 #3

GaryTexmo
Expert 100+
P: 1,501
Well, it worked for me... kind of.

Expand|Select|Wrap|Line Numbers
  1.         private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  2.         {
  3.             ComboBox c = e.Control as ComboBox;
  4.             if (c != null)
  5.             {
  6.                 c.SelectedIndexChanged -= new EventHandler(CBEvent);
  7.                 c.SelectedIndexChanged += new EventHandler(CBEvent);
  8.             }
  9.         }
  10.  
  11.         private void CBEvent(object sender, EventArgs e)
  12.         {
  13.             ComboBox c = sender as ComboBox;
  14.             if (c != null)
  15.             {
  16.                 Console.WriteLine(c.SelectedIndex);
  17.                 button1.Focus();
  18.             }
  19.         }
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 SelectedIndexChanged event and it gets the behaviour you're looking for...

Expand|Select|Wrap|Line Numbers
  1.         private void CBEvent(object sender, EventArgs e)
  2.         {
  3.             ComboBox c = sender as ComboBox;
  4.             if (c != null)
  5.             {
  6.                 dataGridView1.Select();
  7.             }
  8.         }
May 28 '10 #4

P: 6
Hum... I tried so many things that I forgot to try select on the DGV in that event... it works, thanks!
May 31 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.