Reputation: 1550
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?
Upvotes: 10
Views: 58840
Reputation: 33183
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.
That is done by setting the DataPropertyName
of the ComboBoxColumn to the name of a property of the class that the DataGridView is bound against.
Upvotes: 19
Reputation: 41
This worked for me
dim dt as datatable
dt=fillMydata() 'do your function
'dt has 2 col myId as myDescription
Dim col As DataGridViewComboBoxColumn
col = New DataGridViewComboBoxColumn
col.HeaderText = "MyHeader"
col.Name = "Myname"
col.DataSource = data '
col.DisplayMember = "myDescription"
col.ValueMember = "myId"
col.DropDownWidth = 240
''''''''''''''''''''''''''''''''''''''''''''''
'set the value
col.DefaultCellStyle.NullValue = dt.Rows(0).Item("myDescription").ToString
''''''''''''''''''''''''''''''''''''''''''''''
DataGridView1.Columns.Add(col)
hope this help
Upvotes: 3
Reputation: 38230
The error is due to the fact that you have bound a DataSource
to the ComboBox
column and while you assign Select its trying to locate the same in the DataSource
its bound too.
Your best bet will be to add the same to the original DataSource which you bind to the ComboBox
column
Upvotes: 3
Reputation: 3298
There is a way to set the value but it is not allowed if DataSource is set.
DataGridViewComboBoxCell cell =(DataGridViewComboBoxCell)gvList.Rows[0].Cells[0];
cell.Items[0] = "Select";
Upvotes: 5
Reputation: 8646
Try by adding the event
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
}
Upvotes: 0