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

Binding Grid DropDownList to IList

1 Answer 203 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Saju Samuel
Top achievements
Rank 1
Saju Samuel asked on 15 Sep 2009, 07:24 PM
I am using a dropdown field in a RadGrid to edit/save a value to a database field. The edit method is via popup. The dropdown list is populated in code behind with the list of the names associated with the values. This is a general concept used in many business models. The data itself is not important to this discussion, only that I am using name/value pairs. The names should be displayed in the dropdown and the values go back and forth to the database. Here is the aspx code setting up the grid column:
            <telerik:GridDropDownColumn DataField="StartRndType" Visible="false" HeaderText="<%$ Resources:TCRS, Start %>" 
                EditFormColumnIndex="0">  
            </telerik:GridDropDownColumn> 
Here's the code behind:
        protected void grdRndPolicy_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)  
        {  
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
            {  
                // get the editedItem (row)  
                GridEditableItem eeditedItem = e.Item as GridEditableItem;  
                GridEditManager editMan = editedItem.EditManager;  
 
                // get the record  
                RoundPolicyT dataRecord = e.Item.DataItem as RoundPolicyT;  
                // safety check  
                if (dataRecord == null)  
                    return;  
 
                GridDropDownListColumnEditor editor;  
 
                // All 7 rounding type dropdowns use the same item list -  
                // it does not appear to be necessary to make a new copy for each control  
                IList<ListItem> items = new List<ListItem>();  
                items.Add(new ListItem(Resources.TCRS.Forward, "1"));  
                items.Add(new ListItem(Resources.TCRS.Nearest, "2"));  
                items.Add(new ListItem(Resources.TCRS.Backward, "3"));  
 
                // Start Rnd Type dropdown  
                editor = editMan.GetColumnEditor(_STARTRNDTYPE) as GridDropDownListColumnEditor;  
                editor.DataSource = items;  
                editor.DataBind();  
                editor.SelectedValue = dataRecord.StartRndType.ToString();  
The problem I have is that once the editor.DataSource is set, all the Name values are the same as the Value values, so my SelectedValue is not in the list. I have tried using a NameValueCollection instead of an IList but that had no effect. I also tried using a GridDropDownColumnEditor but that had no effect. Somehow setting the DataSource and doing DataBind causes the dropdown list to be populated with just the Name value. The Name/Value should be different (one the item name, one the item value).
My question is, how do I get the dropdown to populate with the Name/Value pairs?

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 16 Sep 2009, 02:52 PM
Hello Saju Samuel,

If you notice, the GridDropDownColumn provide the ListTextField and ListValueField properties that map to DataTextField and DataValueField properties of RadComboBox, respectively. The two indicate the text and value fields that need to go in each RadComboBoxItem object in the RadComboBox.Items collection.

Try setting ListTextField="Text" and ListValueField="Value" in your column. Note that, as the ListItem  object has the Text property specifying its text, and Value property specifying its value, we bind to these two properties, respectively.

Kind regards,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Saju Samuel
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or