Connecting Tech Pros Worldwide Forums | Help | Site Map

C# Forms - DataGridViewComboBoxColumn retaining its selection

Newbie
 
Join Date: Oct 2007
Location: Arizona
Posts: 5
#1: Oct 11 '07
I have DataGridViewComboBoxColumn objects I'm using for some of my cells in a DataGridView control. There must be some kind of editing trick I can do with an event but I'm not having any luck finding an answer to this question, which is:

When I click the drop-down arrow on a combobox, it behaves as if it's already selecting the first value among the items. If I don't explicitly click on the value before tabbing (or clicking on) a different cell, the value that shows up disappears. This is a misleading behavior. Is there a way I can force the combobox to retain the value?

Let me know if I need to provide more information.

Shashi Sadasivan's Avatar
Moderator
 
Join Date: Aug 2007
Location: Brisbane, Australia
Posts: 1,414
#2: Oct 11 '07

re: C# Forms - DataGridViewComboBoxColumn retaining its selection


Hi,

Have you set any propeties for the gridview?
And whats happening in your event handler?

Are you building a web or windows application?
Newbie
 
Join Date: Oct 2007
Location: Arizona
Posts: 5
#3: Oct 11 '07

re: C# Forms - DataGridViewComboBoxColumn retaining its selection


It's a Windows application.

This is basically what I have in a subroutine where I initialize grid properties.

cbcClass is the combobox for the cell

dsClass is a dataset
dtClass is a datatable

Expand|Select|Wrap|Line Numbers
  1. cbcClass.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
  2. dsClass = BookInv.dsGridClass();
  3. dtClass = dsClass.Tables[0];
  4. cbcClass.DataSource = dtClass;
  5.  
  6. dgCheckouts.AllowUserToAddRows = false;
  7. dgCheckouts.AllowUserToDeleteRows = false;
  8. dgCheckouts.AllowUserToOrderColumns = false;
  9. dgCheckouts.MultiSelect = false;
  10. dgCheckouts.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  11. dgCheckouts.EditMode = DataGridViewEditMode.EditOnEnter;
  12.  
  13. dgCheckouts.Columns.Add(cbcClass);
  14. dgCheckouts.Columns[0].HeaderText = "Class";
  15. dgCheckouts.Columns[0].Name = "type_cd";
  16.  
  17. dgCheckouts.Columns[0].Width = 100;
  18.  
I'm not sure which event(s) I need to use although I think that's where I need to deal with it. This is what I have that's not working:

Expand|Select|Wrap|Line Numbers
  1. private void dgCheckouts_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
  2. {
  3.     if (dgCheckouts.CurrentCell.OwningColumn.GetType() != typeof(DataGridViewTextBoxColumn))
  4.     {
  5.         dgCheckouts.CommitEdit(DataGridViewDataErrorContexts.Commit);
  6.     }            
  7. }
  8.  
Thanks for your help.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Oct 11 '07

re: C# Forms - DataGridViewComboBoxColumn retaining its selection


What exactly is the issue?
You have a comboboxcolumn (which means a combo box inside your datagridview yes?)
Is it empty already and you are trying to type values into it and it is thinking you are "done" before you are actually done?

Or does it have data in the column and you want to select one of them?
Newbie
 
Join Date: Oct 2007
Location: Arizona
Posts: 5
#5: Oct 11 '07

re: C# Forms - DataGridViewComboBoxColumn retaining its selection


Quote:

Originally Posted by Plater

You have a comboboxcolumn (which means a combo box inside your datagridview yes?)

Correct

Quote:

Originally Posted by Plater

Is it empty already and you are trying to type values into it and it is thinking you are "done" before you are actually done?

Or does it have data in the column and you want to select one of them?

The combo box has data. If I explicitly click on one of the items in the dropdown list, it works fine. I can tab to a different column and the selection will remain.

[This next part is hard to explain without being able to show you but I'll try]

The behavior I see when I click the dropdown arrow is misleading. When I click on the dropdown arrow, the first selection in the dropdown list fills in the cell as if it was selected and, if that is the selection I want, I'm prone just to click the down arrow again to close the list. The text at this point is displayed in the cell.

Now, when I click on any different cell, the text in the dropdown I just "selected" from disappears because I did not explicitly click the selection in the dropdown list, indicating that, while the text was there, the selected item was not committed. It looks like it does this by design, but I'd like to force it to commit.

I can see end-users getting irritated with that behavior, especially with drop-down lists where the first selection is the most commonly used one.

Let me know if that helps. Sorry if this is clear as mud. I can keep trying.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#6: Oct 11 '07

re: C# Forms - DataGridViewComboBoxColumn retaining its selection


Ok, I think I understand what's happening. Not sure why it's doing it in a windows application.
A possible workaround is to always have the VERY first entry in the dropdown be a blank-line, so when you click the down arrow, your textbox will still be "empty" and nobody would be tempted to just move along without ACTUALLY clicking on an item.

That's not a solution to the problem, but it does provide an alternative.
Newbie
 
Join Date: Oct 2007
Location: Arizona
Posts: 5
#7: Oct 11 '07

re: C# Forms - DataGridViewComboBoxColumn retaining its selection


Yep, you got it.

Thanks for the input. If all else fails I might have to do it that way. It would be funky for me because I'm using a database to draw the values into the combobox and I'm not big on putting a blank as a valid row in database tables, especially when I'm giving users control over what is in them.

If I find the solution I'll post it here.
Reply