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

Populate GridDropDownList

2 Answers 27 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jorge
Top achievements
Rank 1
Jorge asked on 24 Jun 2013, 10:54 PM
How can I get a GridDropDownList to work with entity framework. Specifically how can I attach a field to the DataTextField property and another field to the DataValueField property? Please see the example below.  
 
protected void rgDPR_ItemDataBound(object sender, GridItemEventArgs e)
{
    DbSet<SICCode> sicCodes = DB.SICCodes;
    var query = (from codes in sicCodes
                 orderby codes.Industry ascending
                 select codes.Industry, codes.ID).ToList();
 
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        GridEditManager editMan = editedItem.EditManager;
        GridDropDownListColumnEditor editor = editMan.GetColumnEditor("SICCodeOverride") as GridDropDownListColumnEditor;
        editor.DataSource = query;
        
        editor.DataTextField = ""// codes.Industry should go here
        editor.DataValueField = ""; // codes.ID should go here
 
        editor.DataBind();
    }
}

Thanks in advance.


Jorge

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 25 Jun 2013, 04:02 AM
Hi Jorge,

Please try the following code snippet.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server"  OnItemDataBound="RadGrid1_ItemDataBound">
   <MasterTableView >
         <Columns>
              <telerik:GridEditCommandColumn>
               </telerik:GridEditCommandColumn>                
              <telerik:GridTemplateColumn>
                <EditItemTemplate>
                       <asp:DropDownList ID="DropDownList1" runat="server">
                       </asp:DropDownList>
                </EditItemTemplate>
             </telerik:GridTemplateColumn>
          </Columns>
     </MasterTableView>         
</telerik:RadGrid>


C#:
protected void RadGrid4_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
       {
           GridEditFormItem item = (GridEditFormItem)e.Item;
           DropDownList dropdown = (DropDownList)item.FindControl("DropDownList1");
           //Populate the dropdownlist
           dropdown.DataSourceID = "DataSource1";
           dropdown.DataTextField = "ShipCity";
           dropdown.DataValueField = "ShipCity";
           dropdown.DataBind();          
       }
   }

Thanks,
Princy
0
Jorge
Top achievements
Rank 1
answered on 25 Jun 2013, 03:05 PM
Princy,

Thanks for the speedy reply. Unfortunately the code you supplied didn't work for me. Also, I needed to have 2 different values for the DataTextField property and DataValueField property and I needed to do this using EF. However, I was able to get the code to work the way I wanted. Please find the working code below:

protected void rgDPR_ItemDataBound(object sender, GridItemEventArgs e)
        {
            DbSet<SICCode> sicCodes = DB.SICCodes;
            var query = (from codes in sicCodes
                         orderby codes.Industry ascending
                         select new { codes.Industry, codes.SICCode1 }).ToList();
 
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                GridEditableItem editedItem = e.Item as GridEditableItem;
                GridEditManager editMan = editedItem.EditManager;
                GridDropDownListColumnEditor editor = editMan.GetColumnEditor("SICCodeOverride") as GridDropDownListColumnEditor;
                editor.DataSource = query;
                editor.DataTextField = "Industry";
                editor.DataValueField = "SICCode1";
                editor.DataBind();
            }
        }
Thanks. 


Jorge
Tags
Grid
Asked by
Jorge
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Jorge
Top achievements
Rank 1
Share this question
or