sgray128 1/17/2018 11:51:45 AM

Batch Editing with a DetailTable

This article is a complete working example of a grid that has a detail table in batch editing mode. We show the data population and the BatchEditCommand being used to update the database.

First the aspx:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html>
<head runat="server">
    <form id="form1" runat="server">
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
            <telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false" Width="400px" >
                <MasterTableView EditMode="Batch" CommandItemDisplay="Top" HierarchyDefaultExpanded="true"   Name="MasterGrid" DataKeyNames="OrderNumber">
                    <BatchEditingSettings SaveAllHierarchyLevels="true" />
                        <telerik:GridBoundColumn UniqueName="OrderNumber" HeaderText="OrderNumber" DataField="OrderNumber"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn UniqueName="DocAmount" HeaderText="DocAmount" DataField="DocAmount"></telerik:GridBoundColumn>
                        <telerik:GridTableView ShowHeader="false" DataKeyNames="OrderNumber" EditMode="Batch" HierarchyDefaultExpanded="true" Name="ChildGrid">
                                <telerik:GridBoundColumn UniqueName="Notes" HeaderText="Notes" DataField="Notes"></telerik:GridBoundColumn>

and the code behind:

Imports Telerik.Web.UI
Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
    Private Sub RadGrid1_BatchEditCommand(sender As Object, e As GridBatchEditingEventArgs) Handles RadGrid1.BatchEditCommand
        For Each command As GridBatchEditingCommand In e.Commands
            Dim OrderNumber As String
            '' Logic to extract the values only from the DetailTables in BatchEdit
            Select Case e.Item.OwnerTableView.Name
                Case "ChildGrid"
                    Dim newValues As Hashtable = command.NewValues
                    '' Access the new values
                    Dim Notes As String = newValues("Notes")
                    OrderNumber = newValues("OrderNumber")
                    '' Logic to Save the new values into the database
                Case Else
                    Dim newValues As Hashtable = command.NewValues
                    Dim Notes As String = newValues("DocAmount")
                    OrderNumber = newValues("OrderNumber")
            End Select
    End Sub
    Private Sub RadGrid1_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        Me.RadGrid1.DataSource = GetDateSource()
    End Sub
    Function GetDateSource() As DataTable
        Dim oDT As New DataTable
        'add the columns
        oDT.Columns.Add(New DataColumn("OrderNumber", System.Type.GetType("System.String")))
        oDT.Columns.Add(New DataColumn("DocAmount", System.Type.GetType("System.Double")))
        oDT.Columns.Add(New DataColumn("Notes", System.Type.GetType("System.String")))
        'add rows
        oDT.Rows.Add("ORD123", 1.21, "My order notes")
        oDT.Rows.Add("ORD124", 4, "Some other notes")
        Return oDT
    End Function
    Protected Sub RadGrid1_DetailTableDataBind(sender As Object, e As GridDetailTableDataBindEventArgs) Handles RadGrid1.DetailTableDataBind
        If e.DetailTableView.Name = "ChildGrid" Then
            Dim dataItem As GridDataItem = DirectCast(e.DetailTableView.ParentItem, GridDataItem)
            Dim OrderNumber As String = dataItem.GetDataKeyValue("OrderNumber").ToString()
            e.DetailTableView.DataSource = GetDateSource().Select("OrderNumber = '" & OrderNumber & "'")
        End If
    End Sub
End Class