Click here to Skip to main content
16,012,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am using DataGridView Control To display my object data.

my object is
C#
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; }
}


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
---------------------------
Posted
Updated 31-Mar-13 9:30am
v2
Comments
Kenneth Haugland 31-Mar-13 15:23pm    
WPF or WinForm?
m.kashif.ashraf 31-Mar-13 15:29pm    
WinForm .net 4

1 solution

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

Then you can set ComboBoxColumn.DataPropertyName = "Relation" and attach List to grid's DataSource:
C#
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;

No more DataError
 
Share this answer
 
v2
Comments
m.kashif.ashraf 4-Apr-13 16:35pm    
Thanks Alexander Duzhev.
It worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900