How to select value in DataGridViewComboBoxCell?

I have a DataGridViewComboBoxCell and a DataTable. The data in the table I am associated with a DataGridViewComboBoxCell using a DataSource and setting ValueMember and DisplayMember.

private void Form1_Load(object sender, EventArgs e) { DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell(); dataGridView1.Rows[0].Cells[0] = comboBoxCell; comboBoxCell.DataSource = dataTable; comboBoxCell.ValueMember = "ID"; comboBoxCell.DisplayMember = "Item"; } 

How can I programmatically set a value in a cell when a form loads? In a simple ComboBox, I know the SelectedIndex property. I tried comboBoxCell.Value = ...; but that gives an exception. And tried

 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { e.Value = 1; } 

It sets a new value in the cell, but I need to select a value.

The form is loaded and I have an empty cell.

Form loaded and I have empty cell.

And some data in ComboBox.

And some data in the ComboBox.

When I put this code dataGridView1.Rows[0].Cells["ComboColumn"].Value = "1"; right after comboBoxCell.DisplayMember = ... (see above), it works fine.

The value "1" in the column "Identifier" corresponds to the value "Second" in the column "Elements". So, I get the correct result.

The value "1" in the ID column corresponds to the value "Second" in the Items column.So, I get the correct result.

Sorry for my english and my code for beginners :)

+6
source share
1 answer

Instead of adding a cell to your grid, add a DataGridViewComboBox column.

 DataGridViewComboBoxColumn c = new DataGridViewComboBoxColumn(); c.Name = "ComboColumn"; c.DataSource = dataTable; c.ValueMember = "ID"; c.DisplayMember = "Item"; dataGridView1.Columns.Add(c); 

To select a specific value, you set the Value property for this cell.

 dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = 1; 
  • Please note that this type is important! In the comments, you say you get a System.FormatException . This may be caused by setting the wrong type to value.

    When you set the value to 1, you assign int - if for some reason you have rows in the ID column, you will get a System.FormatException exception that you see.

    If the types are different, you need to either update the DataTable definition or set a value for the string:

     dataGridView1.Rows[rowIndexYouWant].Cells["ComboColumn"].Value = "1"; 
  • Also note that this value must be present in the ID DataTable column that you set as the grid source.

As a rule, it is easiest to work with a DataGridView when it has its own data set. In this case, you can associate the ComboBoxColumn with the DataSource network using the DataPropertyName property.

 c.DataPropertyName = "GridDataSourceColumnName"; 

This allows you to use the value of the columns from the grid data source and for changes to the column to directly change this data source.


Finally, do not use the CellFormatting event here - this event is not intended for such use. It is usually best to do this work in the DataBindingComplete event (if you want it to be done once) or during some event, such as DefaultValues ​​or RowValidating.

Using CellFormatting, you probably won't be able to manually edit the combo box for users.

+15
source

Source: https://habr.com/ru/post/921341/


All Articles