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

Showing string value for enums

8 Answers 171 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 09 Jan 2011, 04:04 AM
Hi.

In a RadGrid control I get an int value and tinyint value from the database.

I need to convert the tinyint value to the string representation of an enum that they represent in my application. E.g. If value = 1 then show "Active", if 2 show "Pending etc"

And with the int value I need to convert it to a value in a database table, the int value essentially being a primary key in another table.

I've looked at the column types page on your demo site (http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/columntypes/defaultcs.aspx) and your support but nothing is showing me what I want except when in edit mode to fill a drop down list.

If you could point me in the right direction I'm sure this is all documentated somewhere.

Many thanks
Chris

8 Answers, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 09 Jan 2011, 04:32 AM
Ok, I now realise that the GridDropDownColumn will do what I want for the database call. What about the tinyint field I mention above?

Thanks. :-)
0
Veli
Telerik team
answered on 13 Jan 2011, 10:21 AM
Hello chrisL,

Yes, I believe you can use the GridDropDownColumn for binding to a lookup table based on values in a foreign key field. Refer to the Customize/Configure GridDropDownColumn help topic for more info on that.

As for the tinyint field you need to show as enumerated values, you can use a GridTemplateColumn to parse the value of your tiny field as an enum. This is for data items in edit mode. For editing such items, you can have a RadComboBox or a DropDownList binding to the values of the enumeration. You then find the dropdown/combo and set its SelectedValue to the data value from the edited item.

This approach is demonstrated in the attached sample page. I used a GridTemplateColumn to bind the data cell to an enumerated value using a binding expression. For the edit form, I defined a RadComboBox whose DataBinding event is used to bind to enum values.

Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Chris
Top achievements
Rank 1
answered on 17 Jan 2011, 06:12 AM
Thanks for this.

What if I have a value that doesn't represent an enum but I want to show as a word in the radgrid? For example, if the value is 0 show "No", 1 show "Yes" or 2 show "Don't know".

Thanks.
0
Veli
Telerik team
answered on 17 Jan 2011, 09:44 AM
You can do it either with a binding expression as demonstrated in the sample project I sent you, or, alternatively, using RadGrid's ItemDataBound event to get the data value and set the respective cell text accordingly:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs)
{
    if(e.Item is GridDataItem)
    {
        GridDataItem gridItem = (GridDataItem)e.Item;
        int myValue = Convert.ToInt32(DataBinder.Eval(gridItem.DataItem, "MyFieldName"));
        if(myValue == 0)
        {
            gridItem["MyColumnName"].Text = "No";
        }
        else if(myValue == 1)
        {
            gridItem["MyColumnName"].Text = "Yes";
        }
        else
        {
            gridItem["MyColumnName"].Text = "Don't know";
        }
    }
}


Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Richard
Top achievements
Rank 1
answered on 22 Oct 2012, 06:23 PM
When you export to excel, that solution will show 1 instead of TinyEnumValue1.  How do we bind the expression for export
0
Kostadin
Telerik team
answered on 25 Oct 2012, 12:42 PM
Hello Richard,

A possible solution is to set the cell text OnItemCreated instead OnItemDataBound. If you that do not prove helpful could you send us you code declaration for further investigation.

Kind regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Richard
Top achievements
Rank 1
answered on 25 Oct 2012, 01:41 PM
I updated my data set to have a new column and I converted my value to it's text there and then just bound to the text.   This allows the easiest use of features.  

This way you can sort by the text, not just the value
You can export

If you only need it to show in the grid, templated column is fine.  But if you really want the full range of features with the least customization and code to maintain, then just modify the data to have the name.

Richard
0
Kostadin
Telerik team
answered on 30 Oct 2012, 08:08 AM
Hi Richard,

Thank you for sharing your approach whit us. It would be useful to our community with the same issue as yours.

Regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Chris
Top achievements
Rank 1
Veli
Telerik team
Richard
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or