Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select a value in a DataGridViewComboBoxCell?

I have DataGridViewComboBoxCell and a DataTable. The data in Table I bound with DataGridViewComboBoxCell using DataSource and set 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 the value in the cell when the form loads? In the simple ComboBox I know a property SelectedIndex. I tried comboBoxCell.Value = ...; but it 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.

Form loaded and I have empty cell.

Form loaded and I have empty cell.

And some data in the 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 ID column corresponds to the value "Second" in the Items column.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 newbie code :)

like image 334
Dima Avatar asked Jul 25 '12 19:07

Dima


People also ask

How to set selected value in DataGridViewComboBoxColumn?

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


1 Answers

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 particular value you set the Value property of a given cell.

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

    When you set the value to 1 you are assigning an int - if for some reason you have strings in the ID column you will get the System.FormatException exception you are seeing.

    If the types differ you need to either update the DataTable definition or set the value to a string:

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

It is usually easiest to work with a DataGridView when it has its DataSource set. In this case you can bind the ComboBoxColumn to the grid's DataSource using the DataPropertyName property.

c.DataPropertyName = "GridDataSourceColumnName";

This allows the columns value to be taken from the grid data source and for changes to the column to directly change that data source.


Lastly, do not use the CellFormatting event here - this event is not intended for this sort of use. It is usually best to do this sort of work in the DataBindingComplete event (if you only want it done once) or during some event like DefaultValues needed or RowValidating.

By using CellFormatting you will probably make it impossible for users to manually edit the combo box.

like image 131
David Hall Avatar answered Sep 19 '22 05:09

David Hall