This is a migrated thread and some comments may be shown as answers.

[Solved] How to Delete Records across pages in a RadGrid.

1 Answer 163 Views
Grid
This is a migrated thread and some comments may be shown as answers.
surama hotta
Top achievements
Rank 1
surama hotta asked on 31 Jul 2009, 07:47 AM
Hi,
I am created a application using Telerik:RadGrid.It performs insert, update, delete operations successfully. I have taken a checkbox and a Button in header of one column and all the rows of that column contain checkboxex. Whenever I select the checkboxed and click on the delete button it deletes the selected records from the grid. When i Select the Header checkbox , it will select all the records of the grid with in that page and by click on the delete button it deletes all the records from that page. 

    I want to delete all the records across the grid when ever i select the header checkbox and click the Delete button instead delete records page wise. Can anybody help me to solve this problem.
Here is my code used to delete records page wise.

 

<telerik:GridTemplateColumn UniqueName="TemplateColumn" ItemStyle-HorizontalAlign = "Center">

 

 

<HeaderStyle Font-Bold = "true" Font-Names = "Verdana" Width = "15%" CssClass = "GridButton"/>

 

 

<HeaderTemplate>

 

 

<asp:CheckBox id="headerChkbox" AutoPostBack="True" runat="server"

 

 

oncheckedchanged="headerChkbox_CheckedChanged"></asp:CheckBox>

 

 

<asp:Button ID = "btnDelete" runat = "server" Text = "Delete" BackColor = "#336699" CssClass = "GridButton" ForeColor = "White" onclick="btnDelete_Click"/>

 

 

</HeaderTemplate>

 



.cs:.

 

protected void headerChkbox_CheckedChanged(object sender, EventArgs e)

 

{

 

if ((sender as CheckBox).Checked)

 

{

 

foreach (GridDataItem dataItem in radSurgicalHistory.MasterTableView.Items)

 

{

(dataItem.FindControl(

"chkRowSelection") as CheckBox).Checked = true;

 

dataItem.Selected =

true;

 

}

}

 

else

 

{

 

foreach (GridDataItem dataItem in radSurgicalHistory.MasterTableView.Items)

 

{

(dataItem.FindControl(

"chkRowSelection") as CheckBox).Checked = false;

 

dataItem.Selected =

false;

 

}

}

}

 

 

protected void chkRowSelection_CheckedChanged(object sender, EventArgs e)

 

{

((sender

as CheckBox).Parent.Parent as GridItem).Selected = (sender as CheckBox).Checked;

 

}

 

 

 

try

 

 

 

 

{

 

if (radSurgicalHistory.MasterTableView.DataKeyNames.Length > 0)

 

{

 

string pkId = "";

 

 

string key = radSurgicalHistory.MasterTableView.DataKeyNames[0];

 

 

for (int i = 0; i < radSurgicalHistory.SelectedItems.Count; i++)

 

{

pkId = radSurgicalHistory.MasterTableView.DataKeyValues[radSurgicalHistory.SelectedItems[i].ItemIndex][key].ToString();

 

string delete = "Update M_SURGICAL_HISTORY SET ROW_STATUS = 0 where Pk_Surgical_History = " + pkId + "";

 

 

SqlHelper.ExecuteNonQuery(Utilities.GetConnectionString, CommandType.Text, delete);

 

}

radSurgicalHistory.Rebind();

}

}

 

catch (Exception ex)

 

{

radSurgicalHistory.Controls.Add(

new LiteralControl("Unable to delete Customers. Reason: " + ex.Message));

 

}

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 05 Aug 2009, 01:47 PM
Hello surama,

One possible option in this case is to handle the logic as follows.
You can attach to the ItemCommand event handler of the grid. There, you can check the status of the header checkbox. If it is checked, you can simply directly delete all the records from the underlying datasource. Then, you can Rebind() the gird, to make sure the changes are properly reflected.
I hope these suggestions get you started properly.

Sincerely yours,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
surama hotta
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or