I'm trying to set a column on my DGV up as a combobox. I've added all the columns to the gridview in the designer so all that is left is to bind them to the dataset.
The datatype of the Status column is varchar.
However i keep getting a very unhelpful error message at run time. Am i doing something wrong?
DataGridViewComboBox Value is not valid.
The above error happens when setting the datasource of the dgv.
dataGridView1.DataSource = JoblistDataSet.Tables["Joblist"];
DataGridViewComboBoxColumn Column = (DataGridViewComboBoxColumn)dataGridView1.Columns["Status"];
Column.DataPropertyName = "Status";
DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)dataGridView1.Rows[0].Cells["Status"];
cbCell.Items.Add("New");
cbCell.Items.Add("Hold");
cbCell.Items.Add("Remove");
dataGridView1.DataSource = JoblistDataSet.Tables["Joblist"];
I think the problem is that you are populating DataGridViewComboBoxCell.Items for row index 0 instead of DataGridViewComboBoxColumn.Items which applies for all rows (hope you noticed Cell
vs Column
).
Use something like this instead
var statusColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns["Status"];
statusColumn.DataPropertyName = "Status";
statusColumn.Items.Add("New");
statusColumn.Items.Add("Hold");
statusColumn.Items.Add("Remove");
// ...