Piotr Śródka Piotr Śródka - 1 year ago 157
ASP.NET (C#) Question

How to bind enum to telerik:RadGrid in ASP WebForms

Can you help me with two way binding: displaying enum values in table and choosing an option in edit mode from drop-down list?

<telerik:GridDropDownColumn DataField="MyEnumProperty" />

With code above it does not display current value in the table nor populate drop-down list (it is empty).

Answer Source

Sorry for answering my own question, but as I got it working... I went with method 2 from @Balaji, but have to make several improvements, so to make it all in one place:

View part:

<telerik:GridTemplateColumn HeaderText="My header" UniqueName="uniqueName" AllowFiltering="true">
    <%#DataBinder.Eval(Container.DataItem, "MyEnumProperty")%>
    <telerik:RadComboBox name="myComboId" Id="myComboId" runat="server">

Code behind:

var dataItem = e.Item as GridEditFormItem;

if (dataItem != null)
        var comboBox = dataItem.FindControl("myComboId") as RadComboBox;

        if (comboBox != null)
            var value = DataBinder.Eval(dataItem.DataItem, "MyEnumProperty").ToString();
            comboBox.DataSource = Enum.GetValues(typeof(MyEnumProperty));
            var selectedItem = comboBox.FindItemByText(value);
            comboBox.SelectedIndex = selectedItem.Index;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download