RadGrid Batch Update with checkbox template column

1 Answer 163 Views
Grid
Fred
Top achievements
Rank 2
Iron
Iron
Iron
Fred asked on 22 Dec 2023, 08:51 PM

I followed the documentation example for using a check box column(see link below).  The issue I'm having is when the BatchEditCommand event fires after the clicking the "Save Changes" item command button, the BatchEditCommand object is always empty so I cannot get the Hash table values to update the record without without using the Open-Edit click event.  

Also, can someone provide a sample of using this method with a RadSwitch instead of a checkbox? 

Thanks in advance for any help.

Editing Check Boxes Directly in Batch Mode

 Markup

<telerik:RadGrid ID="grdCategory" runat="server" AutoGenerateColumns="false" CssClass="Gridheight4" Width="100%" AllowPaging="true" PageSize="7">
      <ClientSettings> 
         <Scrolling AllowScroll="true" UseStaticHeaders="true" />
         <KeyboardNavigationSettings AllowSubmitOnEnter="true" />  
      </ClientSettings>
   <MasterTableView DataKeyNames="Category" EditMode="Batch" HeaderStyle-Font-Size="8pt" HeaderStyle-Font-Bold="true" ItemStyle-Font-Size="8pt"  PagerStyle-AlwaysVisible="true" CommandItemDisplay="Top" InsertItemDisplay="Top"
     AllowAutomaticUpdates="true" AllowAutomaticInserts="true" BatchEditingSettings-EditType="Row" BatchEditingSettings-OpenEditingEvent="Click">
    <CommandItemSettings ShowSaveChangesButton="true" ShowCancelChangesButton="true" ShowRefreshButton="true"/>                                
    <Columns>                                                
      <telerik:GridBoundColumn DataField="Category" UniqueName="Category" HeaderText="Category" DataType="System.String" ForceExtractValue="Always" ItemStyle-CssClass="maximize"></telerik:GridBoundColumn>
      <telerik:GridTemplateColumn DataField="Is_Active" UniqueName="Is_Active" HeaderText="Active" HeaderStyle-Width="80px">
     <ItemTemplate>
        <asp:CheckBox runat="server" ID="CheckBox1" Enabled="true" Checked='<%# Eval("Is_Active") %>' onclick="checkbox1Click(this, event);" />
     </ItemTemplate>
     <EditItemTemplate>
         <asp:CheckBox runat="server" ID="CheckBox2" />
            </EditItemTemplate>
     </telerik:GridTemplateColumn>
     <telerik:GridCheckBoxColumn DataField="CCD_Only" UniqueName="CCD_Only" HeaderText="CCD Only" HeaderTooltip="Show for CCD UMs Only.." DataType="System.Boolean"></telerik:GridCheckBoxColumn>
     <telerik:GridCheckBoxColumn DataField="MMS_Only" UniqueName="MMS_Only" HeaderText="MMS Only" HeaderTooltip="Medication Management Only.."  DataType="System.Boolean"></telerik:GridCheckBoxColumn>
     <telerik:GridBoundColumn DataField="Comments" UniqueName="Comments" HeaderText="Comments" DataType="System.String"></telerik:GridBoundColumn>
     </Columns>
  </MasterTableView>
</telerik:RadGrid>

Client Script

 function checkbox1Click(sender, args) {
      var grid = $find("<%= grdCategory.ClientID %>");
      var batchEditingManager = grid.get_batchEditingManager();
      var parentCell = $telerik.$(sender).closest("td")[0];
      var initialValue = sender.checked;
      batchEditingManager.changeCellValue(parentCell, initialValue);
    }

VB Code Behind

Private Function CategoryData(Optional ByVal Command As String = "", Optional ByVal Category As String = "", Optional ByVal Is_Active As Boolean = True, Optional ByVal CCD_Only As Boolean = False, Optional ByVal MMS_Only As Boolean = False, Optional ByVal Comments As String = "") As DataTable
        Dim strConn As String = ConfigurationManager.ConnectionStrings("UMDB").ToString
        Dim strSQL As String = ""
        Dim dt As DataTable = Nothing

        Select Case Command
            Case PerformInsertCommandName
                strSQL = "EXEC dbo.usp_ins_Lookup_UM_Category "
                strSQL &= "@Category = " & SQL_Prepare_String(Category)
                strSQL &= ", @Is_Active = " & SQL_Prepare_Boolean(Is_Active)
                strSQL &= ", @CCD_Only = " & SQL_Prepare_Boolean(CCD_Only)
                strSQL &= ", @MMS_Only = " & SQL_Prepare_Boolean(MMS_Only)
                strSQL &= ", @Comments = " & SQL_Prepare_String(Comments)
                ExecuteSQL(strSQL, strConn)
                Return dt
            Case UpdateCommandName
                strSQL = "EXEC dbo.usp_upd_Lookup_UM_Category "
                strSQL &= "@Category = " & SQL_Prepare_String(Category)
                strSQL &= ", @Is_Active = " & SQL_Prepare_Boolean(Is_Active)
                strSQL &= ", @CCD_Only = " & SQL_Prepare_Boolean(CCD_Only)
                strSQL &= ", @MMS_Only = " & SQL_Prepare_Boolean(MMS_Only)
                strSQL &= ", @Comments = " & SQL_Prepare_String(Comments)
                ExecuteSQL(strSQL, strConn)
                Return dt
            Case Else
                strSQL = "EXEC dbo.usp_get_Lookup_UM_Category"
                Dim ds As DataSet = GetDataSet(strSQL, 30, strConn)
                dt = ds.Tables(0)
                Return dt
        End Select

    End Function
 '================
 Private Sub grdCategory_PreRender(sender As Object, e As EventArgs) Handles grdCategory.PreRender
        For Each column As GridColumn In grdCategory.Columns
            If column.UniqueName = "Category" Then
                If column.Owner.IsItemInserted Then
                    CType(column, GridEditableColumn).ReadOnly = False
                Else
                    CType(column, GridEditableColumn).ReadOnly = True
                End If
            End If
        Next
        grdCategory.Rebind()

    End Sub
'================
Private Sub grdCategory_BatchEditCommand(sender As Object, e As GridBatchEditingEventArgs) Handles grdCategory.BatchEditCommand
        For Each command As GridBatchEditingCommand In e.Commands
            Dim newValues As Hashtable = command.NewValues
            Dim oldValues As Hashtable = command.OldValues
            Dim strCategory As String = newValues("Category")
            Dim blnActive As Boolean = newValues("Is_Active")
            Dim blnCCDOnly As Boolean = newValues("CCD_Only")
            Dim blnMMSOnly As Boolean = newValues("MMS_Only")
            Dim strComments As String = newValues("Comments")
            If command.Type = GridBatchEditingCommandType.Update Then
                CategoryData("Update", strCategory, blnActive, blnCCDOnly, blnMMSOnly, strComments)
            ElseIf command.Type = GridBatchEditingCommandType.Insert Then
                CategoryData("Insert", strCategory, blnActive, blnCCDOnly, blnMMSOnly, strComments)
            End If
        Next
    End Sub

1 Answer, 1 is accepted

Sort by
0
Accepted
Attila Antal
Telerik team
answered on 27 Dec 2023, 03:35 PM

Hi Fred,

We have created a new KB article with an example of using the Telerik WebForms Switch component in RadGrid with BatchEditing. See Editing Switches directly in Batch Edit Mode.

Regarding the issue:

As long as the Column has the DataField and UniqueName properties set, the BatchEditCommand will contain the new/old values in the Hashtable. If this does not happen, there might be other problems. 

By looking at the code you shared, I assume the AllowAutomaticUpdates and AllowAutomaticInserts properties might be the reason. These properties only work when binding the Grid to data through a Declarative DataSource Control. In the code you shared, I do not see the DataSourceID set anywhere, therefore, I assume that data is bound on the backend. In that case, you will need to make sure that only using the approach from the Programmatic Data Binding Using the NeedDataSource Event article and never call the DataBind() method of the RadGrid instance.

If the issue persists even after following my suggestions, please share the complete implementation of the Grid: Markup + Backend code (including the code that is binding data) and we will look into it.

Regards,
Attila Antal
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
Fred
Top achievements
Rank 2
Iron
Iron
Iron
commented on 27 Dec 2023, 09:19 PM

Thank you for your response Attila, 

I used your KB article on using the Switch component everything is working as expected. You were correct. I was using a programmatic DataSource using the NeedDataSouce event. I also removed the Allow Automatic Updates and Inserts properties from the grid.

Again, thank you for your assistance.   

Tags
Grid
Asked by
Fred
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Attila Antal
Telerik team
Share this question
or