当使用相同的DataSource向DataGridView添加ComboBox列时发生ArgumentException [英] ArgumentException when adding ComboBox column to DataGridView with same DataSource
问题描述
我有一个包含自定义列的 DataGridView
。
但是当我添加一个 DataGridViewComboBoxColumn
,并给它一个我的模型类列表 DataSource
然后我有以下错误:
System.ArgumentException:
DataGridViewComboBoxCell值不是
有效。
新编辑: 4/9/2009 详细信息
我有一个名为的类:SmsPart
具有以下属性:
public class SmsPart
{
public int ID
public SmsPart Parent
public string Name
//和更多
}
我有一个方法叫 GetSmsParts
return List< SmsPart>
。
我想让DataGridView中的 Parent
列为ComboBoxColumn,以选择哪个部分是所选部分的父级。
所以为了这个原因,我做了DataGridViewComboBoxColumn,并设置它的数据源相同的DataSource的洞DataGridView 这是 GetsmsParts方法
DataGridViewComboBoxColumn comboCulomn = new DataGridViewComboBoxColumn();
comboCulomn.DataSource = listParts;
comboCulomn.DataPropertyName =Parent;
comboCulomn.DisplayMember =Name;
comboCulomn.ValueMember =ID;
comboCulomn.Name =Parent;
dgvParts.Columns.Add(comboCulomn);
但我总是有这个错误信息:
< blockquote>
System.ArgumentException:
DataGridViewComboBoxCell值不是
有效。
clm2
/ code>。当你指定值类型是 typeof(smsType)
,你不告诉ComboBox列哪个字段用于该值。
EDIT
稍等一下: smsType
有些复杂的类型吗?我不知道这里是否有任何限制,但是对于一个示例,你应该使用 int
或 string
所以(通常期望存储为数据库字段的任何内容)。
当然, EDIT 2 这意味着在示例中(假设列的数据源包含属性Value和Name): 这就是全部。但是,如果我正确回忆,分配给ValueMember的属性的类型不能是复杂类型(类/结构)... I have a But when I add a " System.ArgumentException:
DataGridViewComboBoxCell value is not
valid.
New EDIT: 4/9/2009 "More Details" I've a class called I've method called " I want the So for that reason I made "DataGridViewComboBoxColumn" and set it's Datasource the same DataSource for the hole DataGridView "Which is the GetsmsParts method": But i always have this error message: System.ArgumentException:
DataGridViewComboBoxCell value is not
valid.
try assigning the name of the datafield for the EDIT Also, of course, the type of the EDIT 2 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... 这篇关于当使用相同的DataSource向DataGridView添加ComboBox列时发生ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! DataGridView
的基础数据源列(在您的示例中称为类型)也必须与
ValueMember
!
$ b
在第二个注释上:想象一个名为tbl的数据库表,其中包含一个名为Type即 Integer
类型。您在DataGridView中显示该表的内容,并且您希望用户能够从组合框中选择类型列的值。这是关于您正在谈论的场景。
DataGridViewComboBoxColumn
中的 Value
字段使用复杂类型。
DataGridViewComboBoxColumn
,您需要为该列分配可能的值列表,并告知该列DataGridView的数据源中存储所选值的字段,该字段为用作显示值,以及哪个字段用作存储在基础数据源列中的值。
DataGridViewComboBoxColumn col = new ...
col.DataSource = columnDataSource;
col.DisplayMember =Name;
col.ValueMember =Value;
col.DataPropertyName =Type;
DataGridView
with custom columns.DataGridViewComboBoxColumn
" and give to it a list of my model class as DataSource
then I had the following error:
SmsPart
has these properties:public class SmsPart
{
public int ID
public SmsPart Parent
public string Name
// and more
}
GetSmsParts
" return "List<SmsPart>
".Parent
column in the DataGridView to be ComboBoxColumn to select which part is the parent of selected part. DataGridViewComboBoxColumn comboCulomn = new DataGridViewComboBoxColumn();
comboCulomn.DataSource = listParts;
comboCulomn.DataPropertyName = "Parent";
comboCulomn.DisplayMember = "Name";
comboCulomn.ValueMember = "ID";
comboCulomn.Name = "Parent";
dgvParts.Columns.Add(comboCulomn);
ValueMember
property of clm2
. While you're specifying that the value type is typeof(smsType)
, you're not telling the ComboBox column which field to use for the value.
Wait a second: Is your smsType
some complex type or something? I'm not sure if any restrictions apply here, but for a sample you should use something like int
or string
or so (anything you'd normally expect to be stored as a database field).DataGridView
's underlying data source's column (in your example called "Type") must also be of the same type as the ValueMember
!
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.
Value
field in a DataGridViewComboBoxColumn
.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.DataGridViewComboBoxColumn col = new ...
col.DataSource = columnDataSource;
col.DisplayMember = "Name";
col.ValueMember = "Value";
col.DataPropertyName = "Type";