DataGridView ComboBoxCell问题 [英] DataGridView ComboBoxCell Issue

查看:75
本文介绍了DataGridView ComboBoxCell问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DataGridView控件来显示我的对象数据。



我的对象是

i am using DataGridView Control To display my object data.

my object is

public class Patient
{
    public string Name { get; set; }
    public Relation Relation { get; set; }
    public int Age { get; set; }
    public string ContactNumber { get; set; }
    public string FatherName { get; set; }
    public string HusbandName { get; set; }
}





我的DataGridView控件包含1个ComboBoxCellColoumn我的对象的Relation属性是enum类型。



问题是,当我将对象的实例分配给DataGridView时,它会给我以下错误



---------------------------

DataGridView默认错误对话框

- --------------------------

DataGridView中发生以下异常:







System.ArgumentException:DataGridViewComboBoxCell值无效。







要替换此默认对话框,请处理DataError事件。

--------------- ------------

OK

--------------------- ------



My DataGridView Control Contains 1 ComboBoxCellColoumn For Relation property of my object which is of enum type.

problem is that when i assign my object''s instance to DataGridView it give me following error

---------------------------
DataGridView Default Error Dialog
---------------------------
The following exception occurred in the DataGridView:



System.ArgumentException: DataGridViewComboBoxCell value is not valid.



To replace this default dialog please handle the DataError event.
---------------------------
OK
---------------------------

推荐答案

您应该将关系枚举中的每个项目添加到ComboBoxColumn项目中:

You should add each item from Relation enum into the ComboBoxColumn Items:
var combo = (grid.Columns["PatientRelation"] as DataGridViewComboBoxColumn);
foreach (var item in Enum.GetValues(typeof(Relation)))
    combo.Items.Add(item);



然后你可以设置ComboBoxColumn.DataPropertyName =Relation并将List附加到网格的数据源:


Then you can set ComboBoxColumn.DataPropertyName = "Relation" and attach List to grid''s DataSource:

var list = new List<Patient>();
list.Add(new Patient(){Relation = Relation.Relation1, Name = "X"});
list.Add(new Patient(){Relation = Relation.Relation2, Name = "Y"});
grid.DataSource = list;



不再有DataError


No more DataError


这篇关于DataGridView ComboBoxCell问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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