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!!!
Asked
Active
Viewed 2,671 times
-2

Jon Egerton
- 40,401
- 11
- 97
- 129

Emmanuel
- 21
- 1
- 3
-
Can you give us some example code? specifically, at what point you want to add in the "Select Item at this point". – Martin Dec 16 '11 at 19:21
2 Answers
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.

Emmanuel
- 21
- 1
- 3
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
.

Rotem
- 21,452
- 6
- 62
- 109
-
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 '11 at 23:24
-
-
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 '11 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 '11 at 14:41
-
-
I just tried it but it didn't work... this is what I got DataGridViewComboBoxCell idProduct; idProducto = row.Cells[0] as DataGridViewComboBoxCell; idProduct.Value = detail[0].IdProduct.ToString(); //Detail is an object from a class that i've created – Emmanuel Dec 20 '11 at 14:48