Hi rrohit9975,
I have created sample code which fulfill your requirement.
HTML
<div>
<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:GridView ID="gvCustomers" AutoGenerateColumns="false" OnPageIndexChanging="OnPageIndexChanging"
AllowPaging="true" PageSize="10" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="checkbox" AutoPostBack="true" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" OnCheckedChanged="check_CheckedChanged" AutoPostBack="true"
runat="server" />
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CustomerId">
<ItemTemplate>
<asp:Label ID="lblCustomerId" Text='<%#Eval("CustomerId")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CustomerName">
<ItemTemplate>
<asp:Label ID="lblCompanyName" Text='<%#Eval("CompanyName")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ContactName">
<ItemTemplate>
<asp:Label ID="lblContactName" Text='<%#Eval("ContactName")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ContactTitle">
<ItemTemplate>
<asp:Label ID="lblContactTitle" Text='<%#Eval("ContactTitle")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" Text='<%#Eval("City")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
C#
private string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
ArrayList CheckBoxArray = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetData();
}
}
private void GetData()
{
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerID,CompanyName,ContactName,ContactTitle,City,Country FROM Customers", con))
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
private void PopulateGridByIndex(int index)
{
this.gvCustomers.PageIndex = index;
this.GetData();
CheckData();
}
protected void check_CheckedChanged(object sender, EventArgs e)
{
int tempIndex = -1;
CheckBox headerCheckbox = (sender as CheckBox);
bool checkedvalue = headerCheckbox.Checked;
ViewState["Uncheck"] = checkedvalue;
tempIndex = gvCustomers.PageIndex;
int pageindex = tempIndex;
for (int i = 0; i <= gvCustomers.PageCount; i++)
{
if (i == gvCustomers.PageCount)
{
PopulateGridByIndex(0);
}
else
{
PopulateGridByIndex(i);
}
string checkAllIndex = "chkAll-" + gvCustomers.PageIndex;
if (checkedvalue)
{
CheckBoxArray.Add(checkAllIndex);
}
foreach (GridViewRow row in gvCustomers.Rows)
{
(row.FindControl("checkbox") as CheckBox).Checked = checkedvalue;
}
((CheckBox)gvCustomers.HeaderRow.Cells[0].FindControl("checkAll")).Checked = checkedvalue;
}
ViewState["CheckBoxArray"] = CheckBoxArray;
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
PopulateGridByIndex(e.NewPageIndex);
CheckData();
}
private void CheckData()
{
if (ViewState["CheckBoxArray"] != null)
{
ArrayList CheckBoxArray = (ArrayList)ViewState["CheckBoxArray"];
string checkAllIndex = "chkAll-" + gvCustomers.PageIndex;
if (CheckBoxArray.IndexOf(checkAllIndex) != -1)
{
foreach (GridViewRow row in gvCustomers.Rows)
{
(row.FindControl("checkbox") as CheckBox).Checked = true;
}
CheckBox chkAll = (CheckBox)gvCustomers.HeaderRow.Cells[0].FindControl("checkAll");
chkAll.Checked = true;
}
}
}
I hope works for you..