This is a migrated thread and some comments may be shown as answers.

Binding GridView to DataTable which contains an Enumeration

4 Answers 327 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Nils
Top achievements
Rank 1
Nils asked on 03 Jan 2009, 03:42 PM
Hey guys,
I'm trying to bind a GridView to a DataTable which contains DataColumns of Type Boolean, String, Int32 and an Enumeration. When I bind it using AutoGenerateColumns = true the column containing the enum-values shows their int representation. I expected it to show as a dropdown like the standard DataGridView does.

Afterwards I tried to create the GridViewColumns myself but I can't figure out how to use the Enum as a DataSource for the GridViewComboBoxColumn. Here is my code:
GridViewComboBoxColumn col5 = new GridViewComboBoxColumn("ResultType"); 
col5.DataSource = Enum.GetValues(typeof(EnumResultType)); 
col5.ValueMember = "???"
col5.DisplayMember = "???"
dgvDisciplines.MasterGridViewTemplate.Columns.Add(col5); 

Is this the right way to specify the DataSource? How do I need to specify the ValueMember and Displaymember?

The DropDown in the ComboBoxEditor shows the right (string) Values but the int-values of the rows aren't converted to that String-Representation.

If needed, here is the code of the enumeration:
public enum EnumResultType 
    { 
        Maximum, 
        Minimum, 
        Summe, 
        Durchschnitt 
    } 

Any hints?

Kind regards,
Nils


4 Answers, 1 is accepted

Sort by
0
Accepted
Victor
Telerik team
answered on 06 Jan 2009, 02:57 PM
Hello Nils,

Thanks for writing.

I suggest wrapping the enum in a class as demonstrated below:
public enum EnumResultType
{  
     Minimum, Maximum, Summe, Durchschnitt  
}  
 
class EnumWrapper  
{  
     public int ID  
     {  
         get;  
         set;  
     }  
 
     public string Name  
     {  
         get { return Enum.GetName(typeof(EnumResultType), ID); }  
         set { ID = (int)Enum.Parse(typeof(EnumResultType), value); }  
     }  
 
     public EnumWrapper(EnumResultType value)  
     {  
          ID = (int)value;  
     }  

Then, follow these steps:
  1. Create a DataTable and a column of type int add to it.
  2. Set the grid DataSource to the DataTable
  3. Create a GridViewComboBoxColumn and bind it to a list of objects of the class above
  4. Add the column to the grid Columns collection.
Here is some sample code:
DataTable table = new DataTable();  
table.Columns.Add("Value"typeof(int));  
 
table.Rows.Add((int)MyEnum.Maximum);  
table.Rows.Add((int)MyEnum.Minimum);  
table.Rows.Add((int)MyEnum.Durchschnitt);  
 
List<EnumWrapper> list = new List<EnumWrapper>();  
list.Add(new EnumWrapper(MyEnum.Durchschnitt));  
list.Add(new EnumWrapper(MyEnum.Maximum));  
list.Add(new EnumWrapper(MyEnum.Minimum));  
list.Add(new EnumWrapper(MyEnum.Summe));  
 
GridViewComboBoxColumn col = new GridViewComboBoxColumn("Value");  
col.DataSource = list;  
col.DisplayMember = "Name";  
col.ValueMember = "ID";  
 
radGridView1.MasterGridViewTemplate.AutoGenerateColumns = false;  
radGridView1.Columns.Add(col);  
radGridView1.DataSource = table; 


Please contact us if you need further assistance.

Regards,
Victor
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Nils
Top achievements
Rank 1
answered on 06 Jan 2009, 06:00 PM
Hi Victor,

thanks. I thought there is an easier method of databinding to a column of an Enum-Type. But your answer gave me the push to the right direction. Because there is more than one column of this kind I wrote a generic class which could be used for every kind of enum. Hopefully it is helpful for anyone who covers the same problem.

Here is my EnumWrapper-class:
class EnumWrapper<t> 
    { 
        public int ID 
        { 
            get
            set
        } 
 
        public string Name 
        { 
            get { return Enum.GetName(typeof(t), ID); } 
            set { ID = (int)Enum.Parse(typeof(t), value); } 
        } 
 
        public EnumWrapper(t value) 
        { 
            ID = (int)Enum.Parse(typeof(t), value.ToString()); 
        } 
 
        public static List<EnumWrapper<t>> EnumToList<t>() 
        { 
 
            Type enumType = typeof(t); 
 
            if (enumType.BaseType != typeof(Enum)) 
                throw new ArgumentException("T must be of type System.Enum"); 
 
            Array enumValArray = Enum.GetValues(enumType); 
            List<EnumWrapper<t>> enumValList = new List<EnumWrapper<t>>(); 
            foreach (int val in enumValArray) 
            { 
                enumValList.Add(new EnumWrapper<t>((t)Enum.Parse(enumType, val.ToString()))); 
            } 
            return enumValList; 
        } 
    } 

And this is the way to use this class as a DataSource for a GridViewcomboBoxColumn:
GridViewComboBoxColumn col5 = new GridViewComboBoxColumn("ResultType"); 
col5.DataSource = EnumWrapper<EnumResultType>.EnumToList<EnumResultType>(); 
col5.ValueMember = "ID"
col5.DisplayMember = "Name"
dgvDisciplines.MasterGridViewTemplate.Columns.Add(col5); 

Nothing more to do :) Thanks!

Kind regards,
Nils



0
Victor
Telerik team
answered on 08 Jan 2009, 08:41 AM
Hi Nils,

Thanks for sharing your solution with the community. We highly appreciate it.
Happy coding! :)

Greetings,
Victor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Michael Bakker
Top achievements
Rank 2
answered on 08 May 2014, 01:04 PM
Thanks for sharing your code. It's still relevant after 5 years!
Tags
GridView
Asked by
Nils
Top achievements
Rank 1
Answers by
Victor
Telerik team
Nils
Top achievements
Rank 1
Michael Bakker
Top achievements
Rank 2
Share this question
or