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

GridDropDownColumn

3 Answers 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mww
Top achievements
Rank 1
mww asked on 15 Oct 2008, 12:27 PM
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#

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 15 Oct 2008, 01:16 PM
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
0
mww
Top achievements
Rank 1
answered on 15 Oct 2008, 05:03 PM
can you provide some examples in c# ?
0
Shinu
Top achievements
Rank 2
answered on 16 Oct 2008, 04:38 AM
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.
Tags
Grid
Asked by
mww
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
mww
Top achievements
Rank 1
Share this question
or