How to set value for DataGridViewComboBox column?

I want to know how to set the value of a DataGridViewComboBox cell. I am already binding a DataGridViewComboBox to a DataSource. But I want to set a new value for this data source.

This is what I still have:

gvList.Rows[0].Cells[0].value = "Select"; 

But it gives an error message: Version exception: cell value of DataGridViewComboBox is not valid .

How can I achieve this without error?

+7
source share
5 answers

The error is due to the fact that you bound the DataSource column to the ComboBox column, and when assigned, select it to try to find the same in its DataSource binding. Itโ€™s best to add the same to the original DataSource that you bind to the ComboBox column

+3
source

The main reason for your problem is that the โ€œSelectโ€ value does not exist in your list of combo box values. You will need to either add this to the data source for the column, or to the data source for the individual list cells (using the edit control for that cell).

The following is a few explanations when setting the value of the selected item that I'm going to leave, as you may find it useful.

There are two main ways to set the value of a DataGridViewComboBoxColumn. You either use data binding or set the value directly.

It looks like you are trying to install directly, so I will explain this first and then below. I will complete the answer with data binding.

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 set the ValueMember property to "id", which refers to the identifier of the property names in the list to which the ComboBox is bound.

So, all I need to do is the following (where I am sure my row and cell indices are correct - you have to write code to check this):

 dataGridView1.Rows[0].Cells[2].Value = 1; 

Now in my example, this works because my ValueMember my id property set, which is an integer, if id was a string property instead, I would need to set the value as follows:

 dataGridView1.Rows[0].Cells[2].Value = "1"; 

(actually just tried this and it is manipulated with implicit casting)

And, of course, I would like to make sure that there really was a value of "1" on my list.

The above lines are the direct cause of your error - your list, which supplies the values โ€‹โ€‹in the combo box, does not contain the value "Select".

To set a value using data binding, you need to tell ComboBoxColumn that it is required in the DataGridView data source.

My setup is DataPropertyName in ComboBoxColumn on the property name of the class that the DataGridView is associated with.

+13
source

There is a way to set the value, but it is not allowed if the DataSource is installed.

 DataGridViewComboBoxCell cell =(DataGridViewComboBoxCell)gvList.Rows[0].Cells[0]; cell.Items[0] = "Select"; 
+3
source

It 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

+2
source

Try adding an event.

 private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) { } 
0
source

All Articles