Click here to Skip to main content
11,922,026 members (56,057 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# .NET .NET4
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; }

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

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)))
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;
No more DataError
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)

  Print Answers RSS
0 OriginalGriff 568
1 Dave Kreskowiak 285
2 Sergey Alexandrovich Kryukov 173
3 Richard MacCutchan 155
4 ppolymorphe 135
0 OriginalGriff 7,036
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,286
3 George Jonsson 2,840
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web03 | 2.8.151120.1 | Last Updated 1 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100