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.
protectedSystem.Web.UI.WebControls.Button chkboxColumnButton;protectedSystem.Web.UI.WebControls.Button chkboxTemplateButton;protectedTelerik.Web.UI.RadGrid RadGrid1;privateHashtable CustomersChecked
{get{object res = ViewState["_cc"];if(res ==null){
res =newHashtable();
ViewState["_cc"]= res;}return(Hashtable)res;}}privatevoidPage_Load(object sender,System.EventArgs e){}overrideprotectedvoidOnInit(EventArgs e){//// CODEGEN: This call is required by the ASP.NET Web Form Designer.//InitializeComponent();base.OnInit(e);}/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>privatevoidInitializeComponent(){this.chkboxColumnButton.Click +=newSystem.EventHandler(this.chkboxColumnButton_Click);this.chkboxTemplateButton.Click +=newSystem.EventHandler(this.chkboxTemplateButton_Click);this.RadGrid1.DeleteCommand +=newTelerik.Web.UI.GridCommandEventHandler(this.RadGrid1_DeleteCommand);this.RadGrid1.NeedDataSource +=newTelerik.Web.UI.GridNeedDataSourceEventHandler(this.RadGrid1_NeedDataSource);this.RadGrid1.ItemDataBound +=newTelerik.Web.UI.GridItemEventHandler(this.RadGrid1_ItemDataBound);this.Load +=newSystem.EventHandler(this.Page_Load);}protectedvoidCheckChanged(object sender,System.EventArgs e){CheckBox box =(CheckBox)sender;GridDataItem item =(GridDataItem)box.NamingContainer;Hashtable target =null;
target = CustomersChecked;if(box.Checked){
target[item["CustomerID"].Text]=true;}else{
target[item["CustomerID"].Text]=null;}}privatevoidRadGrid1_NeedDataSource(object source,Telerik.Web.UI.GridNeedDataSourceEventArgs e){OleDbConnection MyOleDbConnection =newOleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("~/Grid/Data/Access/Nwind.mdb"));OleDbDataAdapter MyOleDbDataAdapter =newOleDbDataAdapter();
MyOleDbDataAdapter.SelectCommand =newOleDbCommand("SELECT TOP 25 CustomerID, ContactName, Address, Country, Bool FROM Customers", MyOleDbConnection);DataTable myDataTable;if(Session["DataSource"]!=null){
myDataTable =(DataTable)Session["DataSource"];}else{
myDataTable =newDataTable();
MyOleDbConnection.Open();try{
MyOleDbDataAdapter.Fill(myDataTable);}finally{
MyOleDbConnection.Close();}
myDataTable.PrimaryKey =newDataColumn[]{ myDataTable.Columns["CustomerID"]};
Session["DataSource"]= myDataTable;}
RadGrid1.DataSource = myDataTable.DefaultView;}privatevoidRadGrid1_DeleteCommand(object source,Telerik.Web.UI.GridCommandEventArgs e){string ID =(e.Item asGridDataItem)["CustomerID"].Text;DataTable table =(DataTable)Session["DataSource"];if(table.Rows.Find(ID)!=null){
table.Rows.Find(ID).Delete();
table.AcceptChanges();}}privatevoidRadGrid1_ItemDataBound(object sender,Telerik.Web.UI.GridItemEventArgs e){if(e.Item isGridDataItem){GridDataItem item = e.Item asGridDataItem;CheckBox box =(CheckBox)item.FindControl("CheckBox1");object isChecked =null;
isChecked = CustomersChecked[item["CustomerID"].Text];if(isChecked !=null){
box.Checked =(bool)isChecked ==true;}}}privatevoidchkboxColumnButton_Click(object sender,System.EventArgs e){foreach(GridItem item in RadGrid1.MasterTableView.Items){if(item isGridDataItem){GridDataItem current = item asGridDataItem;CheckBox box =(CheckBox)current["CheckBoxColumn"].Controls[0];if(box.Checked){string ID = current["CustomerID"].Text;DataTable table =(DataTable)Session["DataSource"];if(table.Rows.Find(ID)!=null){
table.Rows.Find(ID).Delete();
table.AcceptChanges();}}}}
RadGrid1.MasterTableView.Rebind();}privatevoidchkboxTemplateButton_Click(object sender,System.EventArgs e){DataTable table =(DataTable)Session["DataSource"];foreach(DictionaryEntry entry inthis.CustomersChecked){
table.Rows.Find(entry.Key).Delete();}//refresh deleted list
ViewState["_cc"]=null;
table.AcceptChanges();//reset CurrentPageIndex when items count changes to avoid incorrect page index error
RadGrid1.MasterTableView.CurrentPageIndex =0;
RadGrid1.Rebind();}
VB
ProtectedWithEvents RadGrid1 As Telerik.Web.UI.RadGrid
ProtectedWithEvents chkboxColumnButton AsSystem.Web.UI.WebControls.Button
ProtectedWithEvents chkboxTemplateButton AsSystem.Web.UI.WebControls.Button
PrivateSub Page_Init(ByVal sender AsSystem.Object,ByVal e AsSystem.EventArgs)HandlesMyBase.Init
'CODEGEN: This method call is required by the Web Form Designer'Do not modify it using the code editor.
InitializeComponent()EndSubPrivateReadOnlyProperty CustomersChecked()As Hashtable
GetDim res AsObject= ViewState("_cc")If res IsNothingThen
res =New Hashtable
ViewState("_cc")= res
EndIfReturnCType(res, Hashtable)EndGetEndPropertyPrivateSub Page_Load(ByVal sender AsSystem.Object,ByVal e AsSystem.EventArgs)HandlesMyBase.Load
EndSubProtectedSub CheckChanged(ByVal sender AsObject,ByVal e AsSystem.EventArgs)Dim box As CheckBox =CType(sender, CheckBox)Dim item As GridDataItem =CType(box.NamingContainer, GridDataItem)Dim target As Hashtable =Nothing
target = CustomersChecked
If box.Checked Then
target(item("CustomerID").Text)=TrueElse
target(item("CustomerID").Text)=NothingEndIfEndSub'CheckChangedPrivateSub RadGrid1_NeedDataSource(ByVal [source] AsObject,ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)Handles RadGrid1.NeedDataSource
Dim MyOleDbConnection AsNew OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("~/Grid/Data/Access/Nwind.mdb"))Dim MyOleDbDataAdapter AsNew OleDbDataAdapter
MyOleDbDataAdapter.SelectCommand =New OleDbCommand("SELECT TOP 25 CustomerID, ContactName, Address, Country, Bool FROM Customers", MyOleDbConnection)Dim myDataTable As DataTable
IfNot(Session("DataSource")IsNothing)Then
myDataTable =CType(Session("DataSource"), DataTable)Else
myDataTable =New DataTable
MyOleDbConnection.Open()Try
MyOleDbDataAdapter.Fill(myDataTable)Finally
MyOleDbConnection.Close()EndTry
myDataTable.PrimaryKey =New DataColumn(){myDataTable.Columns("CustomerID")}
Session("DataSource")= myDataTable
EndIf
RadGrid1.DataSource = myDataTable.DefaultView
EndSub'RadGrid1_NeedDataSourcePrivateSub RadGrid1_DeleteCommand(ByVal [source] AsObject,ByVal e As Telerik.Web.UI.GridCommandEventArgs)Handles RadGrid1.DeleteCommand
Dim item As GridDataItem = e.Item
Dim ID AsString= item("CustomerID").TextDim table As DataTable =CType(Session("DataSource"), DataTable)IfNot(table.Rows.Find(ID)IsNothing)Then
table.Rows.Find(ID).Delete()
table.AcceptChanges()EndIfEndSub'RadGrid1_DeleteCommandPrivateSub RadGrid1_ItemDataBound(ByVal sender AsObject,ByVal e As Telerik.Web.UI.GridItemEventArgs)Handles RadGrid1.ItemDataBound
IfTypeOf e.Item Is GridDataItem ThenDim item As GridDataItem = e.Item
Dim box As CheckBox =CType(item.FindControl("CheckBox1"), CheckBox)Dim isChecked AsObject=Nothing
isChecked = CustomersChecked(item("CustomerID").Text)IfNot(isChecked IsNothing)Then
box.Checked =CBool(isChecked)=TrueEndIfEndIfEndSub'RadGrid1_ItemDataBoundPrivateSub chkboxColumnButton_Click(ByVal sender AsObject,ByVal e AsSystem.EventArgs)Handles chkboxColumnButton.Click
Dim item As GridItem
For Each item In RadGrid1.MasterTableView.Items
IfTypeOf item Is GridDataItem ThenDim current As GridDataItem = item
Dim box As CheckBox =CType(current("CheckBoxColumn").Controls(0), CheckBox)If box.Checked ThenDim ID AsString= current("CustomerID").TextDim table As DataTable =CType(Session("DataSource"), DataTable)IfNot(table.Rows.Find(ID)IsNothing)Then
table.Rows.Find(ID).Delete()
table.AcceptChanges()EndIfEndIfEndIfNext item
RadGrid1.MasterTableView.Rebind()EndSub chkboxColumnButton_Click
PrivateSub chkboxTemplateButton_Click(ByVal sender AsObject,ByVal e AsSystem.EventArgs)Handles chkboxTemplateButton.Click
Dim table As DataTable =CType(Session("DataSource"), DataTable)Dim entry As DictionaryEntry
For Each entry InMe.CustomersChecked
table.Rows.Find(entry.Key).Delete()Next entry
'refresh deleted list
ViewState("_cc")=Nothing
table.AcceptChanges()'reset CurrentPageIndex when items count changes to avoid incorrect page index error
RadGrid1.MasterTableView.CurrentPageIndex =0
RadGrid1.Rebind()EndSub chkboxTemplateButton_Click