Grid batch editing - Drop Down List loading

1 Answer 44 Views
Grid
Matthew
Top achievements
Rank 1
Iron
Iron
Matthew asked on 06 Sep 2024, 08:29 AM

I would like to use the Grid in batch editing mode in a very similar layout to the https://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/batch-editing/defaultcs.aspx example. But I need to load the RadDropDownList from the .cs. I can not map a asp:SqlDataSource to the nested control.

Please can someone provide me with an example.

TIA.

1 Answer, 1 is accepted

Sort by
0
Accepted
Rumen
Telerik team
answered on 10 Sep 2024, 10:26 AM

Hi Matthew,

To use a RadGrid in batch editing mode and populate a RadDropDownList from the .cs codebehind file (rather than using asp:SqlDataSource), you'll need to leverage the batch editing server-side API. The batch editing mode renders only one editor for the entire column, so the editor will not be available in the OnItemDataBound event, as is the case with other editing modes.

You can access the column editors server-side using the GetBatchColumnEditor method. Below is an example based on your needs, which shows how to bind a RadDropDownList from the .cs code during the grid's PreRender event.

ASPX

<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" GridLines="None" runat="server" 
    AllowAutomaticDeletes="True" AllowAutomaticInserts="True" PageSize="10" 
    OnPreRender="RadGrid1_PreRender" AllowAutomaticUpdates="True" AllowPaging="True"
    EditMode="Batch" OnBatchEditCommand="RadGrid1_BatchEditCommand" DataSourceID="SqlDataSource1">
    <MasterTableView DataKeyNames="ProductID" DataSourceID="SqlDataSource1" EditMode="Batch">
        <Columns>
            <telerik:GridTemplateColumn HeaderText="Category" UniqueName="CategoryID" DataField="CategoryID">
                <ItemTemplate>
                    <%# Eval("CategoryName") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadDropDownList RenderMode="Lightweight" ID="CategoryIDDropDown" runat="server" DataTextField="CategoryName" DataValueField="CategoryID">
                    </telerik:RadDropDownList>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

ASPX.CS

        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {

            // Get the reference to the grid's MasterTableView
            GridTableView masterTable = (sender as RadGrid).MasterTableView;

            // Get the template column editor for the 'CategoryID' column
            GridTemplateColumnEditor categoryEditor = masterTable.GetBatchColumnEditor("CategoryID") as GridTemplateColumnEditor;

            if (categoryEditor != null)
            {
                // Now get the RadDropDownList inside the template column editor
                RadDropDownList ddlCategory = categoryEditor.ContainerControl.FindControl("CategoryIDDropDown") as RadDropDownList;

                if (ddlCategory != null)
                {
                    // Bind the RadDropDownList to your dummy data (or real data)
                    ddlCategory.DataSource = GetCategories(); // Assuming GetCategories() is your data source method
                    ddlCategory.DataTextField = "CategoryName";
                    ddlCategory.DataValueField = "CategoryID";
                    ddlCategory.DataBind();
                }
            }
        }

Here is the DataTable function which generates the dummy datasource for the example:

        private DataTable GetCategories()
        {
            // Create a DataTable to simulate category data
            DataTable categoriesTable = new DataTable();

            // Define columns for CategoryID and CategoryName
            categoriesTable.Columns.Add("CategoryID", typeof(int));
            categoriesTable.Columns.Add("CategoryName", typeof(string));

            // Add some dummy data
            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 the dummy data as a DataTable
            return categoriesTable;
        }

Key Resources:


By using the PreRender event and the GetBatchColumnEditor method, you can load the RadDropDownList data directly from the .cs file without needing an asp:SqlDataSource.

    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
    Matthew
    Top achievements
    Rank 1
    Iron
    Iron
    commented on 11 Sep 2024, 01:55 PM

    Thank you, that works for me
    Tags
    Grid
    Asked by
    Matthew
    Top achievements
    Rank 1
    Iron
    Iron
    Answers by
    Rumen
    Telerik team
    Share this question
    or