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

Tying an enum to a bound column

2 Answers 38 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 30 Jul 2008, 03:14 PM
I have two columns in a datagrid that currently display a number (int) based on the results from a stored procedure.  The numbers in that column, however, are actually matched up to an enum I have defined elsewhere.  What I would like to do is be able to show the text equivalent of the enum instead of the number.  So, for example, if I have an enum as:

Alpha = 1,
Beta,
Delta,
Gamma

and a column of results that look like:
1
3
2
2

the column in the datagrid should display:
Alpha
Delta
Beta
Beta

I hope I'm being concise and clear enough in my question.

Thank you in advance.

2 Answers, 1 is accepted

Sort by
0
Vladimir
Top achievements
Rank 1
answered on 30 Jul 2008, 07:13 PM
Hi Rob,

You can change this value using ItemDataBound event. Just cast e.Item.DataItem to your data item type and set desired cell Text. Here is an example:

if(e.Item is GridDataItem)
{
   DataRowView rowView = ((DataRowView)e.Item.DataItem);
   if(rowView["MyColumn"] == MyValue)
   {
       ((GridDataItem)e.Item)[MyColumnUniqyeName].Text = "MyNewValue";
   }
}
0
Rob
Top achievements
Rank 1
answered on 30 Jul 2008, 09:01 PM
huge thanks!  that definitely helped.  here is the modified solution that i used so that i wouldn't need to make any changes should the enum change...

if (e.Item is Telerik.Web.UI.GridDataItem)  
 
{  
 
DataRowView rowView = ((DataRowView)e.Item.DataItem);  
 
((Telerik.Web.UI.GridDataItem)e.Item)["UniqueColumnName"].Text = Convert.ToString((Enum.Here)Convert.ToInt32(rowView["UniqueColumnName"]));  
 

again, huge thanks!

Tags
Grid
Asked by
Rob
Top achievements
Rank 1
Answers by
Vladimir
Top achievements
Rank 1
Rob
Top achievements
Rank 1
Share this question
or