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

Binding DropDownList inside RadGrid using Batch edit mode in code behind

1 Answer 208 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Diego
Top achievements
Rank 1
Diego asked on 17 Feb 2017, 06:18 PM

I'm having trouble binding values to a DropDownList inside an ItemTemplate in Batch Edit Mode, I tried looking for the control in RadGrid1_PreRender but it returns a null value, I can't use an SQL Data Source because I'm getting the values for the DDL from a WebService, I'd really appreciate some help with this problem, you can find my code below:

<p>public partial class Transform : System.Web.UI.Page     {
        public static CDEC.Interfaz.Instalacion.Contexto.ExcelInfo interfaz = new CDEC.Interfaz.Instalacion.Contexto.ExcelInfo();
        public static object[,] lista = interfaz.Contexto_EmpresaGrupo(179);
  
        protected void Page_Load(object sender, EventArgs e)
        {
  
        }
  
        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            var lista2D = new ExcelInfo(0, 0).OBT_Transformadores_2D();
            this.RadGrid1.DataSource = lista2D;
        }
  
        protected void cbxPropietarioID_PreRender(object sender, EventArgs e)
        {
            //Doesn't seem to work             RadComboBox dropDownList = (RadComboBox)RadGrid1.FindControl("cbxPropietarioID");
        }
  
        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            GridTableView masterTable = (sender as RadGrid).MasterTableView;
  
            //This returns a null             RadDropDownList categoryEditor = masterTable.GetBatchColumnEditor("ID_Propietario") as RadDropDownList;
        }
  
  
    }</p><p></p>
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Transform.aspx.cs" Inherits="InfoTec.Instalacion.Transform" %><%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %><asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">     <br />     
    <link href="styles.css" rel="stylesheet" />     <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" 
        onneeddatasource="RadGrid1_NeedDataSource" PageSize="15" Skin="Simple" 
        onitemdatabound="RadGrid1_ItemDataBound" AllowMultiRowEdit="True" 
        RenderMode="Lightweight" ShowFooter="True" 
        onprerender="RadGrid1_PreRender">         <GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>         <ExportSettings>             <Pdf PageWidth="">             </Pdf>         </ExportSettings>         <MasterTableView CommandItemDisplay="TopAndBottom" DataKeyNames="ID_Trafo_2D" EditMode="Batch">             <CommandItemSettings ShowExportToExcelButton="True" 
                ShowExportToWordButton="True" ShowCancelChangesButton="True" 
                ShowSaveChangesButton="True" />             <Columns>                 <telerik:GridBoundColumn DataField="ID_2D" 
                    FilterControlAltText="Filter column column" HeaderText="2D ID" 
                    UniqueName="column" ReadOnly="True">                     <ItemStyle Width="60px" Wrap="True" />                 </telerik:GridBoundColumn>                 <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn column" 
                    HeaderText="Propietario ID" UniqueName="ID_Propietario" DataField="ID_Propietario">                     <EditItemTemplate>                         <telerik:RadDropDownList ID="ddlPropietarioID" runat="server" 
                            DataTextField="NombrePropietario" DataValueField="ID_Propietario">                         </telerik:RadDropDownList>                     </EditItemTemplate>                     <ItemTemplate>                         <%# Eval("NombrePropietario")%>                     </ItemTemplate>                     <HeaderStyle BackColor="Yellow" />                     <ItemStyle Wrap="False" />                 </telerik:GridTemplateColumn>             </Columns>         </MasterTableView>         <PagerStyle PageSizes="15;30;90" /> 
        <FilterMenu RenderMode="Lightweight"></FilterMenu
        <HeaderContextMenu RenderMode="Lightweight"></HeaderContextMenu>     </telerik:RadGrid>     <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">     </telerik:RadScriptManager></asp:Content>

1 Answer, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 26 Sep 2024, 08:32 AM

Hi everyone,

To achieve this, you can utilize the PreRender event to access the RadDropDownList and bind the data. You can refer to the approach in the Loading RadDropDownList in RadGrid Batch Editing Mode article, which demonstrates how to get a reference to the control in batch mode.

protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    GridTableView masterTable = (sender as RadGrid).MasterTableView;
    GridTemplateColumnEditor categoryEditor = masterTable.GetBatchColumnEditor("CategoryID") as GridTemplateColumnEditor;

    if (categoryEditor != null)
    {
        RadDropDownList ddlCategory = categoryEditor.ContainerControl.FindControl("CategoryIDDropDown") as RadDropDownList;

        if (ddlCategory != null)
        {
            ddlCategory.DataSource = GetCategories();
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataBind();
        }
    }
}

private DataTable GetCategories()
{
    DataTable categoriesTable = new DataTable();
    categoriesTable.Columns.Add("CategoryID", typeof(int));
    categoriesTable.Columns.Add("CategoryName", typeof(string));
    categoriesTable.Rows.Add(1, "Beverages");
    categoriesTable.Rows.Add(2, "Condiments");
    categoriesTable.Rows.Add(3, "Confections");
    categoriesTable.Rows.Add(4, "Dairy Products");
    categoriesTable.Rows.Add(5, "Grains/Cereals");

    return categoriesTable;
}

This approach ensures that you’re binding the RadDropDownList in the PreRender event using batch editing mode. Make sure the DataField for the GridTemplateColumn in your markup corresponds to the correct field names in your data source.

You can also refer to the Cascading MultiColumnComboBox in Grid with BatchEdit article if you need more advanced scenarios like cascading or multi-column dropdowns within a batch-edit grid.

Regards,
Rumen
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
Tags
Grid
Asked by
Diego
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Share this question
or