ArgumentException的增加组合框列的DataGridView具有相同的数据源时,组合、数据源、ArgumentException、DataGridView

2023-09-04 22:35:43 作者:宽带你的世界。

我有一个的DataGridView 自定义列。

但是,当我添加一个 DataGridViewComboBoxColumn ,并给它我的模型类的列表,数据源然后我有以下错误:

  

System.ArgumentException:   的DataGridViewComboBoxCell值不   有效的。

新的编辑:的 2009/4/9的更多细节的

我有一类叫做 SmsPart 有这些属性:

 公共类SmsPart
{
    公众诠释ID
    公共SmsPart父
    公共字符串名称
    // 和更多
}
 

我有办法叫 GetSmsParts 重返名单,其中,SmsPart>

我要的在DataGridView列被ComboBoxColumn选择哪一部分是选择部分的家长。

因此​​,对于这个原因,我提出了DataGridViewComboBoxColumn,并将其设置的数据源相同的数据源洞的DataGridView ,这是 GetsmsParts法

  DataGridViewComboBoxColumn comboCulomn =新DataGridViewComboBoxColumn();
    comboCulomn.DataSource = listParts;
    comboCulomn.DataPropertyName =父;
    comboCulomn.DisplayMember =名称;
    comboCulomn.ValueMember =ID;
    comboCulomn.Name =父;
    dgvParts.Columns.Add(comboCulomn);
 

但我一直有这样的错误信息:

  

System.ArgumentException:   的DataGridViewComboBoxCell值不   有效的。

解决方案

尝试分配数据字段为 CLM2 。当你指定的值类型是的typeof(smsType),你不告诉组合框列的字段使用的值。

修改 等一下:您 smsType 一些复杂型还是什么?我不知道如果有限制这里,但你应该使用类似 INT 字符串或样品所以(什么你通常​​期望被存储为一个数据库字段)。

此外,当然,的DataGridView 的基础数据源的列(称为类型在你的例子)的类型也必须是相同的类型的 ValueMember

编辑2 关于你的第二个评论:想象一下,被称为任务型的数据库表,其中包含(其中包括)一列名为类型,类型为整数的。你显示该表的内容在你的DataGridView,你希望用户能够选择的值从组合框中的类型列。这是关于你正在谈论的场景。

这是不可能的存储复杂类型就像你正在使用的数据库列之一,所以你不能在使用复杂类型的字段 DataGridViewComboBoxColumn 。 要执行的数据对整个电网的结合上,必须将网格数据库表TBL。要创建 DataGridViewComboBoxColumn ,你需要的可能值列表分配给列,并告诉列在DataGridView的数据源领域是存储选定值,其中场用作显示值和该场被用作存储在底层数据源的列的值。 关于VC 6.0,往列表框里添加数据的问题

这意味着样品中(假设数据源列包含属性价值和名称):

  DataGridViewComboBoxColumn COL =新...
col.DataSource = columnDataSource;
col.DisplayMember =名称;
col.ValueMember =值;
col.DataPropertyName =类型;
 

这是所有。然而,属性的类型分配给ValueMember不能是一个复杂类型(类/结构),如果我没有记错...

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 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.

EDIT 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).

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 the ValueMember!

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.

it is not possible to store complex types in like the one you're using in database columns, so you can not use complex types for the Value field in a DataGridViewComboBoxColumn. To perform the data binding for the entire grid, you must bind the grid to the database table "tbl". To create the 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"):

DataGridViewComboBoxColumn col = new ...
col.DataSource = columnDataSource;
col.DisplayMember = "Name";
col.ValueMember = "Value";
col.DataPropertyName = "Type";

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...