I want to know how to set the value of a DataGridViewComboBox cell. I already bind the DataGridViewComboBox with DataSource. But I want to set new value to this this datasource.
This is what i have so far:
gvList.Rows[0].Cells[0].value = "Select";
But it throws an error saying: Format Exception: DataGridViewComboBox Cell value is not valid .
How could I achieve this without error?
The root cause of your problem is that the value "Select" does not exist in your list of combo box values. You will either need to add this to the datasource for the column, or to the datasource for the individual cell's combobox (using the editing control of that cell).
Below is some more explanation on setting the value of the selected item which I'm going to leave as you might find it useful.
There are two basic ways to set the value of a DataGridViewComboBoxColumn. You either use databinding or you set the value directly.
It sounds like you are attempting to set directly, so I'll explain that first and then below I'll complete the answer with databinding.
Taking the following contrived example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BindingList<User> users = new BindingList<User> { new User() { UserName = "Fred", userid = 2 } };
IList<MyValue> values = new List<MyValue> { new MyValue{id = 1, name="Fred"}, new MyValue{id = 2, name="Tom"}};
dataGridView1.DataSource = users;
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.DataSource = values;
col.DisplayMember = "name";
col.DataPropertyName = "userid";
col.ValueMember = "id";
dataGridView1.Columns.Add(col);
}
}
public class MyValue
{
public int id { get; set; }
public string name { get; set; }
}
public class User
{
public string UserName { get; set; }
public int userid {get;set;}
}
In this example, I have set the ValueMember
property to be "id" which refers to the property names id in the list my ComboBox is bound to.
So all I need to do is the following (where I am sure that my row and cell indexes are correct - you should write code to check that):
dataGridView1.Rows[0].Cells[2].Value = 1;
Now, in my example this works because my ValueMember
is set to my "id" property which is an integer, if instead id was a string property I would need to set value like this:
dataGridView1.Rows[0].Cells[2].Value = "1";
(actually just tried this and it manages with an implicit cast)
And of course, I would want to make sure that there was actually a value of "1" in my list.
The above few lines are the direct cause of your error - your list that supplies the values to the combo box does not contain the value "Select".
To set the value using databinding, you need to tell the ComboBoxColumn what it is binding against in the DataGridView datasource.
The is done my setting the DataPropertyName of the ComboBoxColumn to the name of a property of the class that the DataGridView is bound against.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With