Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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.
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.
Sorry for my English and my newbie code :)
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.
–
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.