Ron Hagan

Ron Hagan

  • NA
  • 1
  • 0

Set the SelectedItem in a DataGridViewComboBoxCell

Mar 10 2010 1:34 PM
Hi,

I have a Windows application, written in c# and in my application, I have a DataGridView that has 2 DataGridViewComboBoxColumns in it, along with a DataGridViewTextBoxColumn.  The 2 ComboBoxColumns are each populated by a DataSet.  For each row, the user selects an item from each of the ComboBoxColumns and enters a number into the TextBoxColumn.  As an example, let's say the Color ComboBox has 3 choices in it:  Red, Blue & Green and the Size ComboBox has 3 choices in it:  Small, Medium & Large.  In our example, for the first row, the user has selected Red from the Color ComboBox, Medium for the Size ComboBox and has entered "5" in the Qty TextBox.  For Row 2, the user selected Green in that Color ComboBox, Large in the Size ComboBox and has entered "4" in the Qty TextBox.


 Row     Color(ComboBox)               Size(ComboBox)                   Qty(TextBox)
-----------------------------------------------------------------------------------------------
   1            Red                                      Medium                                    5
   2            Green                                   Large                                       4

Once the user is done, this information is saved to a table.  Let's also assume that this information was for January, 2010.

When the user starts a new month, (February, 2010, for example), their data could be exactly the same as January or it may change a little.  If it IS the same or very close to the same, rather than force the user to reenter all of the information as they did in January, I'd like to be able to give them the option to  "import" the data from January but still give them all the options that they had in January.  What this means is that the Color ComboBoxColumn for each row still has Red, Blue & Green as its items that can be chosen, but for Row1, "Red" needs to be the selected item and for Row 2, "Green" needs to be the selected item.  Keeping with that same idea, the Size ComboBox still has Small, Medium and Large as its items that can be selected for each row, but for Row 1, "Medium" needs to be set as the selected item and for Row 2, "Large" needs to be the selected item.

My DataGridView would be populated the exact same way as it did in January but the selected items that show would be the data that the user had chosen in January.  Because I'm retrieving that data (what the user chose in Jan) from the database, I know that in the DataGridView cell for Row1, Color ComboBox, the selected item should be "Red".  Since I know that, how do I set the selected item in that ComboBox to "Red"?  If this was a straight ComboBox, I know how to set the SelectedItem (or SelectedIndex).  But because this is a DataGridViewComboBox, it doesn't seem to work the same way.

Any ideas or suggestions will be greatly appreciated.  If more information is needed in order to help me solve this, please let me know.

Thanks in advance.

Ron

Answers (2)