-2

I would like to set a value to the ComboBox in the DataGridView. I already have changed the comboBoxItems, I just want to select one of them. Thank you in advance!!!

1
  • Can you give us some example code? specifically, at what point you want to add in the "Select Item at this point". Dec 16, 2011 at 19:21

2 Answers 2

1

I already solved my problem... I'm gonna post the way I did and hoppefully someone will find this answer too.

   dgrDetalle.DataSource = dataTable("select * from yourTable");
   DataTable dtCombo = dataTableCombo("select COL_ID DETOC_COL_FK,COL_DESCRIPCION from yourTable2");
   string[] strColumns = new string[] { "COL_DESCRIPCION" };
   MultiColumnDictionary map = new MultiColumnDictionary(dtCombo, "DETOC_COL_FK", strColumns, 0);
   dgrDetalle.Cols["DETOC_COL_FK"].DataMap = map;

As you can see the class that save my life is MultiColumnDictionary.

Note: The combobox items must be loaded in a different DatatTable than the DataTable that is gonna load directly in the grid.

0

As far as I know, the Comboboxes only actually exist as controls when they are being edited, and therefore don't have a selected item property.
You can simply set the Value property of the cell to the item you want selected, or alternitively, you can set a default value by setting the property:

DataGridViewColumn.DefaultCellStyle.NullValue.

6
  • Thanks for the answer. I already did what you say, I only can access to the comboBox.selectedValue when I am editing it, buy what I want to do now is retrieve some data from a database, and I do not have any problem with the DataGridViewTextBoxCell.Value Assignation, the data are loaded to the DataGridView, but not the same situation for the DataGridViewComboBoxCell. I even tried with DataGridViewComboBoxCell.Value and this didn't work. Hope someone can help me. Anyway Thank You so much!!
    – Emmanuel
    Dec 16, 2011 at 23:24
  • What type of value are you assigning? A string or an integer?
    – Rotem
    Dec 17, 2011 at 10:34
  • It is an Integer... I´m using Oracle, so the data type from the database is "number", and when I make the query I take the value from that field on the table and I handle it as an Integer.
    – Emmanuel
    Dec 19, 2011 at 14:29
  • The error that shows a Dialod is this: The following exception ocurred in the DataGridView: System.FormatException: DataGridViewComboBoxCell value is not valid. To replace this default dialog please handle the DataError event.
    – Emmanuel
    Dec 19, 2011 at 14:41
  • the value type must be a string.
    – Rotem
    Dec 20, 2011 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.