New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Deleting Grid Items Depending on CheckBox State

The example below represents how to delete grid items depending on GridCheckBoxColumn state and GridTemplateColumn with checkbox as ItemTemplate. There are two separate buttons on the page which do the job.For the first case, you need to traverse the grid items and delete them from the grid source if the checkbox in the GridCheckBoxColumn cell is checked. This is done when the user presses the Remove default-checked rows button and will be propagated for the current page only.For the second case the main idea is to use CustomersChecked ViewState property in which to save the current checkbox state for the GridTemplateColumn. You will also need to subscribe to the CheckedChanged event of the checkbox and update CustomersChecked. Actually changes are made if the corresponding checkbox has been checked by the user (otherwise this property is not modified for ViewState optimization). When the user hits the Remove user-checked rows button each checked entry in CustomersChecked is deleted from the grid datas ource and the CurrentPageIndex is reset. Note that in this case the checked state is persisted on paging and checked items are deleted in all available pages.

The default delete functionality for each row through DeleteColumn button is also supported in this example.

ASP.NET
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" Style="z-index: 101; left: 48px; position: absolute;
  top: 32px" runat="server" AutoGenerateColumns="False" AllowSorting="True" PageSize="13"
  AllowPaging="True">
  <MasterTableView>
    <Columns>
      <telerik:GridBoundColumn Display="False" UniqueName="CustomerID" ReadOnly="True"
        HeaderText="CustomerID" DataField="CustomerID" />
      <telerik:GridCheckBoxColumn UniqueName="CheckBoxColumn" ReadOnly="True" HeaderText="CheckBox Column"
        DataField="Bool" />
      <telerik:GridBoundColumn UniqueName="ContactName" SortExpression="ContactName" HeaderText="ContactName"
        DataField="ContactName" />
      <telerik:GridBoundColumn UniqueName="Address" SortExpression="Address" HeaderText="Address"
        DataField="Address" />
      <telerik:GridBoundColumn UniqueName="Country" SortExpression="Country" HeaderText="Country"
        DataField="Country" />
      <telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Check/Uncheck box">
        <ItemTemplate>
          <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckChanged">
          </asp:CheckBox>
        </ItemTemplate>
      </telerik:GridTemplateColumn>
      <telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete">
      </telerik:GridButtonColumn>
    </Columns>
  </MasterTableView>
  <PagerStyle Mode="NumericPages"></PagerStyle>
</telerik:RadGrid>
<asp:Button ID="chkboxColumnButton" Style="z-index: 103; left: 776px; position: absolute;
  top: 112px" runat="server" Text="Remove default-checked rows"></asp:Button>
<asp:Button ID="chkboxTemplateButton" Style="z-index: 104; left: 776px; position: absolute;
  top: 160px" runat="server" Text="Remove User-checked rows"></asp:Button>
Not finding the help you need?
Contact Support