当使用相同的DataSource向DataGridView添加ComboBox列时发生ArgumentException [英] ArgumentException when adding ComboBox column to DataGridView with same DataSource

查看:275
本文介绍了当使用相同的DataSource向DataGridView添加ComboBox列时发生ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含自定义列的 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 所以(通常期望存储为数据库字段的任何内容)。



当然, DataGridView 的基础数据源列(在您的示例中称为类型)也必须与 ValueMember
$ b

EDIT 2

在第二个注释上:想象一个名为tbl的数据库表,其中包含一个名为Type即 Integer 类型。您在DataGridView中显示该表的内容,并且您希望用户能够从组合框中选择类型列的值。这是关于您正在谈论的场景。


  1. 无法像您使用的一样存储复杂类型数据库列,因此您不能对 DataGridViewComboBoxColumn 中的 Value 字段使用复杂类型。

  2. 要对整个网格执行数据绑定,必须将网格绑定到数据库表tbl。要创建 DataGridViewComboBoxColumn ,您需要为该列分配可能的值列表,并告知该列DataGridView的数据源中存储所选值的字段,该字段为用作显示值,以及哪个字段用作存储在基础数据源列中的值。

这意味着在示例中(假设列的数据源包含属性Value和Name):

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

这就是全部。但是,如果我正确回忆,分配给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.

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

这篇关于当使用相同的DataSource向DataGridView添加ComboBox列时发生ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆