TelerikDeveloper.com

TelerikDeveloper.com

sgray128 4/4/2018 10:06:38 AM

Editing in BATCH mode

(sigh) using the Telerik ASP.NET grid in BATCH edit mode can be challenging. 

Below is a working example...

 



Lessons learned:

You have to use template columns to get dropdowns populated. 

Templates with more than one control will confuse the grid when you switch back and forth between edit mode. 

There is no setting the selected value of a dropdown. The grid sets it automatically. If the template only has one control. 

Markup for the grid:

<telerik:RadGrid ID="RadGrid1" runat="server" skin="Bootstrap" AutoGenerateColumns="false" RenderMode="Auto"   >
    <MasterTableView DataKeyNames="DEX_ROW_ID, itmclscd,zBudgetElement,zVendorID,zUnitCost" EditMode="Batch"
        CommandItemDisplay="TopAndBottom" CommandItemSettings-ShowAddNewRecordButton="false"   >
        <SortExpressions>
            <telerik:GridSortExpression FieldName="itmclscd"  />
        </SortExpressions>
        <GroupHeaderTemplate>
            <asp:Label ID="lblItemClass" runat="server" Text='<%# Bind("itmclscd") %>' ></asp:Label>
        </GroupHeaderTemplate>
        <GroupByExpressions>
            <telerik:GridGroupByExpression>
                <SelectFields>
                    <telerik:GridGroupByField HeaderText="Status" FieldName="itmclscd" ></telerik:GridGroupByField>
                </SelectFields>
                <GroupByFields >
                    <telerik:GridGroupByField FieldName="itmclscd"  />
                </GroupByFields>
            </telerik:GridGroupByExpression>
        </GroupByExpressions>
        <Columns>
            <telerik:GridTemplateColumn HeaderText="Vendor/Item" HeaderStyle-Width="50%" UniqueName="ColVendorTemplate"  >
                <ItemTemplate>
                    <asp:Label SkinID="Bootstrap"  ID="lblVendName" Text='<%# Eval("vendname") %>' runat="server"></asp:Label><br />
                    <asp:Label SkinID="Bootstrap"  ID="lblzBudgetElement" Text='<%# Eval("zBudgetElement2") %>' runat="server" ></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    Vendor ID <br /><telerik:RadDropDownList Skin="Bootstrap" Width="300px" ID="ddlVendorID" runat="server" ></telerik:RadDropDownList>
                </EditItemTemplate>
            </telerik:GridTemplateColumn
                           
            <telerik:GridTemplateColumn HeaderText="Unit" HeaderStyle-Width="25%" UniqueName="colUnitCost" >
                <ItemTemplate>
                    <asp:Label SkinID="Bootstrap"  ID="Label3" Text='<%# Eval("zUnitCost", "{0:f2}")  %>' runat="server" ></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    Unit Cost <br /><telerik:RadNumericTextBox Skin="Bootstrap" ID="ntbUnitCost"  NumberFormat-DecimalDigits="2" runat="server" ></telerik:RadNumericTextBox>
                </EditItemTemplate>
            </telerik:GridTemplateColumn>               
             
            <telerik:GridTemplateColumn HeaderText="Qty/Tot" HeaderStyle-Width="25%" UniqueName="colUnitQty" >
                <ItemTemplate>
                    <asp:Label SkinID="Bootstrap"  ID="Label1" Text='<%# Eval("zQty", "{0:f2}")  %>' runat="server" ></asp:Label><br />
                    <asp:Label SkinID="Bootstrap"  ID="Label2" Text='<%# Eval("zCostAmt", "{0:f2}")  %>' runat="server" ></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>               
 
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

Code behind:

Imports Telerik.Web.UI
Public Class Test
    Inherits InheritedPage
    Dim mstrProjectID As String
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("intMasterNumber") Is Nothing Then
            Response.Redirect("Default.aspx")
        End If
 
        Me.lblMasterNumber.Text = String.Format("Master: {0}", Session("intMasterNumber"))
 
        'get the project
        Dim oDT As DataTable = SPs.fp_PC00200_SEL_byNUF(Session("intMasterNumber"), "ngb01").getTable
        If oDT.Rows.Count <> 1 Then
            Response.Redirect("SalesQueue.aspx")
        End If
 
        mstrProjectID = oDT.Rows(0)("zProjectID")
        lblProjectID.Text = String.Format("Project: {0}", mstrProjectID)
 
    End Sub
    Private Sub RadGrid1_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        RadGrid1.DataSource = SPs.fp_MasonRates_SEL(mstrProjectID, "ngb01").getTable
    End Sub
    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
 
            'if this line is in edit mode
            Dim item As GridEditableItem = e.Item
            'access/modify the edit item template settings here
            Dim ddlVendorID As RadDropDownList = item.FindControl("ddlVendorID")
            ddlVendorID.DataSource = SPs.fp_PM00200_SEL_forLookup("MASONS", oAppUser.Database).getTable
            ddlVendorID.DataValueField = "vendorid"
            ddlVendorID.DataTextField = "vendname"
            ddlVendorID.DataBind()
            ddlVendorID.SelectedValue = e.Item.DataItem("zVendorID")
 
            Dim ntbUnitCost As RadNumericTextBox = item.FindControl("ntbUnitCost")
            ntbUnitCost.Text = e.Item.DataItem("zUnitCost")
 
        End If
 
    End Sub
    Private Sub RadGrid1_BatchEditCommand(sender As Object, e As GridBatchEditingEventArgs) Handles RadGrid1.BatchEditCommand
        For Each command As GridBatchEditingCommand In e.Commands
 
            Dim newValues As Hashtable = command.NewValues
            Dim oldValues As Hashtable = command.OldValues
            Dim intDexRowID = newValues("DEX_ROW_ID")
            Dim ntbUnitCost As RadNumericTextBox = RadGrid1.FindControl(RadGrid1.MasterTableView.ClientID + "_colUnitCost").FindControl("ntbUnitCost")
            Dim ddlVendorID As RadDropDownList = RadGrid1.FindControl(RadGrid1.MasterTableView.ClientID + "_ColVendorTemplate").FindControl("ddlVendorID")
 
            Dim decUnitCost As Decimal = newValues("zUnitCost")
            If Not ntbUnitCost.Value Is Nothing Then
                decUnitCost = ntbUnitCost.Value
            End If
            SPs.fp_MasonRatesSetVendor_UPD2(intDexRowID, ddlVendorID.SelectedValue, decUnitCost, oAppUser.Database).execute()
        Next
 
        Response.Redirect("someplaceelse.aspx")
 
    End Sub
    Private Sub RadGrid1_PreRender(sender As Object, e As EventArgs) Handles RadGrid1.PreRender
        Dim ddlVendorID As RadDropDownList = RadGrid1.FindControl(RadGrid1.MasterTableView.ClientID + "_ColVendorTemplate").FindControl("ddlVendorID")
 
        ddlVendorID.DataSource = SPs.fp_PM00200_SEL_forLookup("MASONS", oAppUser.Database).getTable
        ddlVendorID.DataValueField = "vendorid"
        ddlVendorID.DataTextField = "vendname"
        ddlVendorID.DataBind()
    End Sub
 
End Class