简体   繁体   中英

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:RadGrid>
    <MasterTableView>
    <Columns>
        <telerik:GridDropDownColumn DataField="MyEnumProperty" />
    </Columns>
<telerik:RadGrid>

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

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">
<ItemTemplate>
    <%#DataBinder.Eval(Container.DataItem, "MyEnumProperty")%>
</ItemTemplate>
<EditItemTemplate>
    <telerik:RadComboBox name="myComboId" Id="myComboId" runat="server">
    </telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>

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));
            comboBox.DataBind();
            var selectedItem = comboBox.FindItemByText(value);
            comboBox.SelectedIndex = selectedItem.Index;
        }
    }
}

You can try this

Method 1 : (you cannot do this for your case)

 <telerik:RadGrid ID="grid">
            <MasterTableView>
            <Columns>
                <telerik:GridDropDownColumn UniqueName="drpColumn" DataField="MyEnumProperty" />
            </Columns>
        <telerik:RadGrid>

Method 2 :

 <telerik:GridTemplateColumn HeaderText="dropdown" UniqueName="drpColumn" AllowFiltering="true">
    <ItemTemplate>
        <%#DataBinder.Eval(Container.DataItem, "textTodisplay")%>
    </ItemTemplate>
    <EditItemTemplate>
        <telerik:RadComboBox ID="ddlForEdit" runat="server">
        </telerik:RadComboBox>
    </EditItemTemplate>
    </telerik:GridTemplateColumn>

Considering, on the top of methods inside class you have enums

 enum ddlElements
 {
   a, b, c, d
 };

Then in GridItemDataBound event

protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
    GridDataItem item = (GridDataItem)e.Item;
    GridEditableItem editItem = e.Item as GridEditableItem;
    if (e.Item is GridEditableItem && e.Item.IsInEditMode) // Only when the grid is in EDIT MODE
    {
RadComboBoxItem selectedItem = new RadComboBoxItem(); 
RadComboBox editor= (RadComboBox)grid["drpColumn"].FindControl("ddlForEdit");
roleName = DataBinder.Eval(myGridItem.DataItem, "drpColumn").ToString();
editor.DataSource = Enum.GetValues(typeof(ddlElements));
editor.DataBind();
selectedItem = combo.FindItemByText(roleName);
editor.SelectedIndex = selectedItem.Index;        
    }
}

Or you can also try using the template column for making the dropdown list in your grid.

UPDATE :

Try the template column Method 2 , please remove the code which you already used. The code which is written in the ItemDataBound event will be for edit mode. It will display the dropdownlist only at the time when a user clicks on the edit button.

In Normal mode(Non-Editable grid) by default, either you need to have a value for that particular column or it should be simply left blank. If you have a value from database then you can bind it using DataBinder.Eval , so when in normal mode it will display the data from db and in edit mode, it will bind the enum values given from the ItemDataBound event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM