GridDropDownColumn

4 posts, 0 answers
  1. mww
    mww avatar
    257 posts
    Member since:
    Sep 2012

    Posted 15 Oct 2008 Link to this post

    How can I populate the 

    GridDropDownColumn in my codebehind (c#), ive found examples where the datasourceid is set in the aspx page like this

    <telerik:GridDropDownColumn  
                                             UniqueName="DropDownListCategory" 
                                             ListTextField="Category" 
                                             ListValueField="Category" 
                                             DataSourceID="SqlDataSource2" 
                                             HeaderText="Category" 
                                             DataField="Category" 
                                             DropDownControlType="RadComboBox" 
                                             AllowSorting="true">  
                                            </telerik:GridDropDownColumn> 
     But the contents of the dropdown can change depending on a selection elsewhere on the page, so Id like to be able to set the datasource using c#
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 15 Oct 2008 Link to this post

    Hi Mark,

    The built-in GridDropDownColumn is designed to be used mainly with DataTables in order to easily map and generate their dropdown items values in conjunction with the DataField/ListValueField/DataSourceID/ListDataMember values of the column.

    For changing the source of the dropdown editor dynamically, consider replacing the GridDropDownColumn with template column holding MS DropDownList in its edit template. This will allow to generating/binding the items for the DropDownList inside the ItemDataBound handler of RadGrid or change the DataTextField/DataValueField/DataSourceID of the
    dropdown editor.

    Thanks
    Shinu
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. mww
    mww avatar
    257 posts
    Member since:
    Sep 2012

    Posted 15 Oct 2008 Link to this post

    can you provide some examples in c# ?
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 15 Oct 2008 Link to this post

    Hi Mark,

    Here is the code snippet to set the DataSource of a DropDownList in the EditItemTemplate of a  GridTemplateColumn.

    ASPX:
    <telerik:GridTemplateColumn    HeaderText="TempCol"  DataField="ProductName"  UniqueName="TempCol" > 
     <ItemTemplate> 
         <asp:Label ID="Label1" runat="server" Text='<%#Eval("ProductName") %>' ></asp:Label> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <asp:DropDownList ID="DropDownList1"  runat="server"
         </asp:DropDownList> 
     </EditItemTemplate> 
      
    </telerik:GridTemplateColumn> 

    CS:
     
     protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)  
        {  
            string connectionstring = "Data Source=Admin.inc.com; Initial Catalog=NorthWind;User ID=admin; Password=admininc";  
            SqlConnection conn = new SqlConnection(connectionstring);  
            conn.Open();  
            SqlDataAdapter adp = new SqlDataAdapter("select * from  Products ", conn);  
            DataTable dt = new DataTable();  
            adp.Fill(dt);  
            if ((e.Item is GridEditableItem)&&(e.Item.IsInEditMode))  
            {  
                GridEditableItem item = (GridEditableItem)e.Item;  
                DropDownList ddl = (DropDownList)item["TempCol"].FindControl("DropDownList1");  
                ddl.DataSource = dt;  
                ddl.DataTextField = "ProductID";  
                ddl.DataValueField = "ProductID";  
            }  
              
        }  


    Thanks
    Shinu.
Back to Top