I have a DataGridView with custom columns.
But when I add a “DataGridViewComboBoxColumn” and give to it a list of my model class as DataSource then I had the following error:
System.ArgumentException:
DataGridViewComboBoxCell value is not
valid.
New EDIT: 4/9/2009 “More Details”
I’ve a class called SmsPart has these properties:
public class SmsPart
{
public int ID
public SmsPart Parent
public string Name
// and more
}
I’ve method called “GetSmsParts” return “List<SmsPart>“.
I want the Parent column in the DataGridView to be ComboBoxColumn to select which part is the parent of selected part.
So for that reason I made “DataGridViewComboBoxColumn” and set it’s Datasource the same DataSource for the hole DataGridView “Which is the GetsmsParts method“:
DataGridViewComboBoxColumn comboCulomn = new DataGridViewComboBoxColumn();
comboCulomn.DataSource = listParts;
comboCulomn.DataPropertyName = "Parent";
comboCulomn.DisplayMember = "Name";
comboCulomn.ValueMember = "ID";
comboCulomn.Name = "Parent";
dgvParts.Columns.Add(comboCulomn);
But i always have this error message:
System.ArgumentException:
DataGridViewComboBoxCell value is not
valid.
try assigning the name of the datafield for the
ValueMemberproperty ofclm2. While you’re specifying that the value type istypeof(smsType), you’re not telling the ComboBox column which field to use for the value.EDIT
Wait a second: Is your
smsTypesome complex type or something? I’m not sure if any restrictions apply here, but for a sample you should use something likeintorstringor so (anything you’d normally expect to be stored as a database field).Also, of course, the type of the
DataGridView‘s underlying data source’s column (in your example called “Type”) must also be of the same type as theValueMember!EDIT 2
On your second comment: Imagine a database table called “tbl” that contains (among others) one column called “Type” that is of type
Integer. You’re displaying the contents of that table in your DataGridView and you want the user to be able to select values for the “Type” column from a combo box. This is about the scenario you’re talking about.Valuefield in aDataGridViewComboBoxColumn.DataGridViewComboBoxColumn, you need to assign a list of possible values to the column and tell the column the field in the DataGridView’s datasource it stores the selected value to, which field is used as a display value and which field is used as the value that is stored in the underlying datasource’s column.That means in the sample (assuming the data source for the column contains properties “Value” and “Name”):
This is all. However, the type of the property you assign to “ValueMember” can not be a complex type (class/struct) if I recall correctly…