C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

Delete selected rows in different pages of gridview control using Asp.net


Posted Date: 19 Oct 2009    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: Srikanth ReddyMember Level: Silver    
Rating: 1 out of 5Points: 15




Take one ASP.Net GridView with paging enabled which means you can provide user to select records on multiple rows on multiple pages also and delete all selected rows or records on all the pages all at once.

Prerequest::



.one Gridview control
.one Hidden field
.one Button control
.sql database table with some content to bind data into gridview control


HTML page looks like:



<asp:GridView ID="gvAll" runat="server"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" AllowPaging ="true"
OnPageIndexChanging = "OnPaging" DataKeyNames = "XXXXXXX"
PageSize = "10" >

<Columns>

<asp:TemplateField>
<HeaderTemplate>

<asp:CheckBox ID="chkAll" runat="server"
onclick = "checkAll(this);" />

</HeaderTemplate>
<ItemTemplate>

<asp:CheckBox ID="chk" runat="server"
onclick = "Check_Click(this)"/>

</ItemTemplate>
</asp:TemplateField>

<asp:BoundField ItemStyle-Width = "150px" DataField = "XYZ"
HeaderText = "XYZ"/>

<asp:BoundField ItemStyle-Width = "150px" DataField = "ABC"
HeaderText = "ABC"/>

<asp:BoundField ItemStyle-Width = "150px" DataField = "123"
HeaderText = "123"/>

</Columns>

<AlternatingRowStyle BackColor="#C2D69B" />

</asp:GridView>

<asp:HiddenField ID="hfCount" runat="server" Value = "0" />

<asp:Button ID="btnDelete" runat="server" Text="Delete Checked Records"
OnClientClick = "return ConfirmDelete();" OnClick="btnDelete_Click" />


Bind data in GridView control



private void BindGrid()
{
string constr = ConfigurationManager
.ConnectionStrings["conString"].ConnectionString;
string query = "select * from Table1";
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
sda.Fill(dt);
gvAll.DataSource = dt;
gvAll.DataBind();

}

Maintaining the state of Checkboxes while paging



-To facilitate this i used following two functions
1. GetData 2.SetData

-The GetData function simply retrieves the records for which the user has checked the checkbox, adds them to an ArrayList and then saves the ArrayList to ViewState
-The SetData function simply restores the saved state of the checkboxes from the ViewState


private void GetData()
{
ArrayList arr;

if (ViewState["SelectedRecords"] != null)
arr = (ArrayList)ViewState["SelectedRecords"];
else
arr = new ArrayList();

CheckBox chkAll = (CheckBox)gvAll.HeaderRow
.Cells[0].FindControl("chkAll");

for (int i = 0; i < gvAll.Rows.Count; i++)
{
if (chkAll.Checked)
{
if (!arr.Contains(gvAll.DataKeys[i].Value))
{
arr.Add(gvAll.DataKeys[i].Value);
}
}
else
{
CheckBox chk = (CheckBox)gvAll.Rows[i]
.Cells[0].FindControl("chk");
if (chk.Checked)
{
if (!arr.Contains(gvAll.DataKeys[i].Value))
{
arr.Add(gvAll.DataKeys[i].Value);
}
}
else
{
if (arr.Contains(gvAll.DataKeys[i].Value))
{
arr.Remove(gvAll.DataKeys[i].Value);
}
}
}
}
ViewState["SelectedRecords"] = arr;
}

/*GetData is called in the Page Load event of the ASP.Net web page in the following way*/

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
GetData();
BindGrid();
}

// setdata function

private void SetData()
{
int currentCount = 0;
CheckBox chkAll = (CheckBox)gvAll.HeaderRow
.Cells[0].FindControl("chkAll");
chkAll.Checked = true;
ArrayList arr = (ArrayList)ViewState["SelectedRecords"];
for (int i = 0; i < gvAll.Rows.Count; i++)
{
CheckBox chk = (CheckBox)gvAll.Rows[i]
.Cells[0].FindControl("chk");
if (chk != null)
{
chk.Checked = arr.Contains(gvAll.DataKeys[i].Value);
if (!chk.Checked)
chkAll.Checked = false;
else
currentCount++;
}
}
hfCount.Value = (arr.Count - currentCount).ToString();
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Using C#  .  Select & delete multiple rows in different pages of grid view control in asp.net  .  Gridview row delete  .  Delete selected rows in different pages of gridview control using c#.net  .  Delete multiple rows in multiple pages at once in asp.net gridview control  .  Delete multiple rows  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Date Validation in GridView
Previous Resource: Adding rows to grid view at runtime
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use