CheckBox Checked status while Pagination of Gridview in ASP.net...


In this article I'm trying to explain the gridview checked status while pagination. We have a gridview and we have a no.of Records in that then we go for Pagination of gridview. While paging we loose the checked status of the gridview. To overcome this issue I give some suggestion to maintain the status of checked.

GridViiew Checked status:



In this article I'm trying to explain the gridview checked status while pagination. We have a gridview and we have a no.of Records in that then we go for Pagination of gridview. While paging we loose the checked status of the gridview. To overcome this issue I give some suggestion to maintain the status of checked.

Steps to Achieve this:



1) Open new website and give a name for that as GVCheckedStatus
2) Open a newitem by right clicking on project then choose newitem then choose new website.
3) Simply drag & drop a gridview control and design that in below manner

Source Code:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GVCheckedStatus.aspx.cs" Inherits="GVCheckedStatus" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
font-family: "Trebuchet MS";
font-size: x-large;
color: #333300;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<br />
<br />

</div>
<div align="center" style="color: #000066">

<span class="style1">GridView with Checked Status</span><br />
<br />
<br />
<br />
<table>
<asp:GridView ID="GV" runat="server" AutoGenerateColumns="false"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="white" OnRowDataBound="GV_OnRowDataBound"
AllowPaging="True" onpageindexchanging="GV_PageIndexChanging" PageSize="5" >
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Employee Number" DataField="empno" ReadOnly="true" />
<asp:BoundField HeaderText="Employee Name" DataField="ename" />
<asp:BoundField HeaderText="Job" DataField="job" />
<asp:BoundField HeaderText="Manager" DataField="mgr" />
<asp:BoundField HeaderText="HireDate" DataField="hiredate" ReadOnly="true" />
<asp:BoundField HeaderText="Salary" DataField="sal" />
<asp:BoundField HeaderText="Commission" DataField="comm" />
</Columns>
</asp:GridView>
</table>
</div>
</form>
</body>
</html>



4) After design a page you just write a code for that.

Code Behind:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class GVCheckedStatus : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("DataBase=ENGSOFT;User id=sa;Password=P@ssword9");
SqlDataAdapter da;
DataSet ds;
DataTable dt;
SqlCommand cmd;
int DeptNo=1;
static int no = 0;


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind_GV();
}
}

protected void Bind_GV()
{

con.Open();
da = new SqlDataAdapter("select empno, ename,job,mgr,hiredate,sal,comm,picture from emp where DEPTNO=" + DeptNo, con);
dt = new DataTable();
da.Fill(dt);

con.Close();
if (dt.Rows.Count > 0)
{
GV.DataSource = dt;
GV.DataBind();
}
else
{
DataRow dr = dt.NewRow();

dt.Rows.Add(dr);
dt.AcceptChanges();
GV.DataSource = dt;
GV.DataBind();
GV.Rows[0].Visible = false;
}
}


5) Using this event we keep the status of checkbox.


protected void GV_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = new DataTable();
if (ViewState["dt"] != null)
dt = ViewState["dt"] as DataTable;
if (dt.Rows.Count > 0)
{
DataRow[] dr=dt.Select("empno="+e.Row.Cells[1].Text);
if (dr.Length > 0)
(e.Row.FindControl("chk") as CheckBox).Checked = true;
}
}
}


6) using this method we just store the checked status in one datatable and store it in ViewState and using that we keep maintain the status of this


protected void Checked_Status()
{
DataTable dt = new DataTable();
if (ViewState["dt"] != null)
dt = ViewState["dt"] as DataTable;
foreach (GridViewRow row in GV.Rows)
{
if ((row.FindControl("chk") as CheckBox).Checked == true)
{
if (dt.Rows.Count == 0)
dt.Columns.Add("empno");

DataRow dr = dt.NewRow();
dr["empno"] = row.Cells[1].Text;
dt.Rows.Add(dr);
dt.AcceptChanges();
}
}
ViewState["dt"] = dt;
}


7) OnPageIndexChanged event you just call that method.

protected void GV_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Checked_Status();
GV.PageIndex = e.NewPageIndex;
Bind_GV();
}
}



OutPut:



Page1

Page2

Conclusion:



During this process we can maintain the state of the checkbox. Hope this article will help those who are looking for the same.


Attachments

  • Source Code (45146-214721-Source-Code.zip)
  • Article by naveensanagasetti
    I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

    Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: