Hi vijay9471,
Check this example. Now please take its reference and correct your code.
For check all checkboxes selection in all pages it is not possible.
Click on header checkbox then update record and go to other page and again check the header checkbox and update record. Then on update button click all the changes will be updated in database.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"
AllowPaging="true" PageSize="4" DataKeyNames="CustomerId" OnPageIndexChanging="gvCustomers_PageIndexChanging"
OnPageIndexChanged="gvCustomers_PageIndexChanged">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Name") %>' Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
<asp:DropDownList ID="ddlCountries" runat="server" Visible="false">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="Update" Visible="false" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="Delete" Visible="false" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Linq
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers");
gvCustomers.DataSource = this.ExecuteQuery(cmd, "SELECT");
gvCustomers.DataBind();
// Initialize the ViewState.
if (ViewState["UpdatedRecord"] == null)
{
ViewState["UpdatedRecord"] = this.ExecuteQuery(cmd, "SELECT").Clone();
}
}
private DataTable ExecuteQuery(SqlCommand cmd, string action)
{
string conString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Connection = con;
switch (action)
{
case "SELECT":
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
return null;
}
}
protected void Update(object sender, EventArgs e)
{
DataTable dt = ViewState["UpdatedRecord"] as DataTable;
if (dt != null)
{
// Adding the current index record to ViewState.
for (int i = 0; i < gvCustomers.Rows.Count; i++)
{
GridViewRow row = gvCustomers.Rows[i];
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
if (isChecked)
{
string name = row.Cells[1].Controls.OfType<TextBox>().FirstOrDefault().Text;
string country = row.Cells[2].Controls.OfType<DropDownList>().FirstOrDefault().SelectedItem.Value;
int id = Convert.ToInt32(gvCustomers.DataKeys[row.RowIndex].Value);
dt.Rows.Add(id, name, country);
}
}
// Loop through the records in ViewState and update the changes in database.
for (int i = 0; i < dt.Rows.Count; i++)
{
SqlCommand cmd = new SqlCommand("UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @CustomerId");
cmd.Parameters.AddWithValue("@Name", dt.Rows[i]["Name"]);
cmd.Parameters.AddWithValue("@Country", dt.Rows[i]["Country"]);
cmd.Parameters.AddWithValue("@CustomerId", dt.Rows[i]["CustomerId"]);
this.ExecuteQuery(cmd, "SELECT");
}
}
else
{
foreach (GridViewRow row in gvCustomers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
if (isChecked)
{
string name = row.Cells[1].Controls.OfType<TextBox>().FirstOrDefault().Text;
string country = row.Cells[2].Controls.OfType<DropDownList>().FirstOrDefault().SelectedItem.Value;
int id = Convert.ToInt32(gvCustomers.DataKeys[row.RowIndex].Value);
SqlCommand cmd = new SqlCommand("UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @CustomerId");
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Parameters.AddWithValue("@CustomerId", id);
this.ExecuteQuery(cmd, "SELECT");
}
}
}
}
btnUpdate.Visible = false;
btnDelete.Visible = false;
this.BindGrid();
ViewState["UpdatedRecord"] = null;
}
protected void Delete(object sender, EventArgs e)
{
foreach (GridViewRow row in gvCustomers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
if (isChecked)
{
SqlCommand cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerId = @CustomerId");
cmd.Parameters.AddWithValue("@CustomerId", gvCustomers.DataKeys[row.RowIndex].Value);
this.ExecuteQuery(cmd, "SELECT");
}
}
}
btnUpdate.Visible = false;
btnDelete.Visible = false;
this.BindGrid();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlCommand cmd = new SqlCommand("SELECT DISTINCT(Country) FROM CustomerTest11");
DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
ddlCountries.DataSource = this.ExecuteQuery(cmd, "SELECT");
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
string country = (e.Row.FindControl("lblCountry") as Label).Text;
ddlCountries.Items.FindByValue(country).Selected = true;
}
}
protected void OnCheckedChanged(object sender, EventArgs e)
{
bool isUpdateVisible = false;
CheckBox chk = (sender as CheckBox);
if (chk.ID == "chkAll")
{
foreach (GridViewRow row in gvCustomers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
}
}
}
CheckBox chkAll = (gvCustomers.HeaderRow.FindControl("chkAll") as CheckBox);
chkAll.Checked = true;
foreach (GridViewRow row in gvCustomers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
for (int i = 1; i < row.Cells.Count; i++)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
}
if (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked;
}
if (isChecked && !isUpdateVisible)
{
isUpdateVisible = true;
}
if (!isChecked)
{
chkAll.Checked = false;
}
}
}
}
btnUpdate.Visible = isUpdateVisible;
btnDelete.Visible = isUpdateVisible;
}
protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvCustomers.PageIndex = e.NewPageIndex;
// Adding updated rows to ViewState.
if (ViewState["UpdatedRecord"] != null)
{
DataTable dtUpdated = ViewState["UpdatedRecord"] as DataTable;
for (int i = 0; i < gvCustomers.Rows.Count; i++)
{
GridViewRow row = gvCustomers.Rows[i];
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
if (isChecked)
{
int id = Convert.ToInt32(gvCustomers.DataKeys[row.RowIndex].Value);
DataRow dr = dtUpdated.Select("CustomerId=" + id).Length > 0 ? dtUpdated.Select("CustomerId=" + id)[0] : null;
if (dr != null)
{
dtUpdated.Rows.Remove(dr);
}
string name = row.Cells[1].Controls.OfType<TextBox>().FirstOrDefault().Text;
string country = row.Cells[2].Controls.OfType<DropDownList>().FirstOrDefault().SelectedItem.Value;
dtUpdated.Rows.Add(id, name, country);
ViewState["UpdatedRecord"] = dtUpdated;
}
}
}
this.BindGrid();
}
protected void gvCustomers_PageIndexChanged(object sender, EventArgs e)
{
if (ViewState["UpdatedRecord"] != null)
{
DataTable dtUpdated = ViewState["UpdatedRecord"] as DataTable;
foreach (GridViewRow row in gvCustomers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
int id = Convert.ToInt32(gvCustomers.DataKeys[row.RowIndex].Value);
DataRow dr = dtUpdated.Select("CustomerId=" + id).Length > 0 ? dtUpdated.Select("CustomerId=" + id)[0] : null;
if (dr != null)
{
row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = true;
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
for (int i = 1; i < row.Cells.Count; i++)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Text = dr["Name"].ToString();
}
if (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked;
row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().ClearSelection();
row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Items.FindByValue(dr["Country"].ToString()).Selected = true;
}
}
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers")
gvCustomers.DataSource = Me.ExecuteQuery(cmd, "SELECT")
gvCustomers.DataBind()
If ViewState("UpdatedRecord") Is Nothing Then
ViewState("UpdatedRecord") = Me.ExecuteQuery(cmd, "SELECT").Clone()
End If
End Sub
Private Function ExecuteQuery(ByVal cmd As SqlCommand, ByVal action As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constring").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
cmd.Connection = con
Select Case action
Case "SELECT"
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Select
Return Nothing
End Using
End Function
Protected Sub Update(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = TryCast(ViewState("UpdatedRecord"), DataTable)
If dt IsNot Nothing Then
For i As Integer = 0 To gvCustomers.Rows.Count - 1
Dim row As GridViewRow = gvCustomers.Rows(i)
Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
If isChecked Then
Dim name As String = row.Cells(1).Controls.OfType(Of TextBox)().FirstOrDefault().Text
Dim country As String = row.Cells(2).Controls.OfType(Of DropDownList)().FirstOrDefault().SelectedItem.Value
Dim id As Integer = Convert.ToInt32(gvCustomers.DataKeys(row.RowIndex).Value)
dt.Rows.Add(id, name, country)
End If
Next
For i As Integer = 0 To dt.Rows.Count - 1
Dim cmd As SqlCommand = New SqlCommand("UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @CustomerId")
cmd.Parameters.AddWithValue("@Name", dt.Rows(i)("Name"))
cmd.Parameters.AddWithValue("@Country", dt.Rows(i)("Country"))
cmd.Parameters.AddWithValue("@CustomerId", dt.Rows(i)("CustomerId"))
Me.ExecuteQuery(cmd, "SELECT")
Next
Else
For Each row As GridViewRow In gvCustomers.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
If isChecked Then
Dim name As String = row.Cells(1).Controls.OfType(Of TextBox)().FirstOrDefault().Text
Dim country As String = row.Cells(2).Controls.OfType(Of DropDownList)().FirstOrDefault().SelectedItem.Value
Dim id As Integer = Convert.ToInt32(gvCustomers.DataKeys(row.RowIndex).Value)
Dim cmd As SqlCommand = New SqlCommand("UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @CustomerId")
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Country", country)
cmd.Parameters.AddWithValue("@CustomerId", id)
Me.ExecuteQuery(cmd, "SELECT")
End If
End If
Next
End If
btnUpdate.Visible = False
btnDelete.Visible = False
Me.BindGrid()
ViewState("UpdatedRecord") = Nothing
End Sub
Protected Sub Delete(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvCustomers.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
If isChecked Then
Dim cmd As SqlCommand = New SqlCommand("DELETE FROM Customers WHERE CustomerId = @CustomerId")
cmd.Parameters.AddWithValue("@CustomerId", gvCustomers.DataKeys(row.RowIndex).Value)
Me.ExecuteQuery(cmd, "SELECT")
End If
End If
Next
btnUpdate.Visible = False
btnDelete.Visible = False
Me.BindGrid()
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cmd As SqlCommand = New SqlCommand("SELECT DISTINCT(Country) FROM CustomerTest11")
Dim ddlCountries As DropDownList = (TryCast(e.Row.FindControl("ddlCountries"), DropDownList))
ddlCountries.DataSource = Me.ExecuteQuery(cmd, "SELECT")
ddlCountries.DataTextField = "Country"
ddlCountries.DataValueField = "Country"
ddlCountries.DataBind()
Dim country As String = (TryCast(e.Row.FindControl("lblCountry"), Label)).Text
ddlCountries.Items.FindByValue(country).Selected = True
End If
End Sub
Protected Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim isUpdateVisible As Boolean = False
Dim chk As CheckBox = (TryCast(sender, CheckBox))
If chk.ID = "chkAll" Then
For Each row As GridViewRow In gvCustomers.Rows
If row.RowType = DataControlRowType.DataRow Then
row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked = chk.Checked
End If
Next
End If
Dim chkAll As CheckBox = (TryCast(gvCustomers.HeaderRow.FindControl("chkAll"), CheckBox))
chkAll.Checked = True
For Each row As GridViewRow In gvCustomers.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
For i As Integer = 1 To row.Cells.Count - 1
row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Visible = Not isChecked
If row.Cells(i).Controls.OfType(Of TextBox)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Visible = isChecked
End If
If row.Cells(i).Controls.OfType(Of DropDownList)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of DropDownList)().FirstOrDefault().Visible = isChecked
End If
If isChecked AndAlso Not isUpdateVisible Then
isUpdateVisible = True
End If
If Not isChecked Then
chkAll.Checked = False
End If
Next
End If
Next
btnUpdate.Visible = isUpdateVisible
btnDelete.Visible = isUpdateVisible
End Sub
Protected Sub gvCustomers_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
gvCustomers.PageIndex = e.NewPageIndex
If ViewState("UpdatedRecord") IsNot Nothing Then
Dim dtUpdated As DataTable = TryCast(ViewState("UpdatedRecord"), DataTable)
For i As Integer = 0 To gvCustomers.Rows.Count - 1
Dim row As GridViewRow = gvCustomers.Rows(i)
Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
If isChecked Then
Dim id As Integer = Convert.ToInt32(gvCustomers.DataKeys(row.RowIndex).Value)
Dim dr As DataRow = If(dtUpdated.[Select]("CustomerId=" & id).Length > 0, dtUpdated.[Select]("CustomerId=" & id)(0), Nothing)
If dr IsNot Nothing Then
dtUpdated.Rows.Remove(dr)
End If
Dim name As String = row.Cells(1).Controls.OfType(Of TextBox)().FirstOrDefault().Text
Dim country As String = row.Cells(2).Controls.OfType(Of DropDownList)().FirstOrDefault().SelectedItem.Value
dtUpdated.Rows.Add(id, name, country)
ViewState("UpdatedRecord") = dtUpdated
End If
Next
End If
Me.BindGrid()
End Sub
Protected Sub gvCustomers_PageIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
If ViewState("UpdatedRecord") IsNot Nothing Then
Dim dtUpdated As DataTable = TryCast(ViewState("UpdatedRecord"), DataTable)
For Each row As GridViewRow In gvCustomers.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim id As Integer = Convert.ToInt32(gvCustomers.DataKeys(row.RowIndex).Value)
Dim dr As DataRow = If(dtUpdated.[Select]("CustomerId=" & id).Length > 0, dtUpdated.[Select]("CustomerId=" & id)(0), Nothing)
If dr IsNot Nothing Then
row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked = True
Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
For i As Integer = 1 To row.Cells.Count - 1
row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Visible = Not isChecked
If row.Cells(i).Controls.OfType(Of TextBox)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Visible = isChecked
row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Text = dr("Name").ToString()
End If
If row.Cells(i).Controls.OfType(Of DropDownList)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of DropDownList)().FirstOrDefault().Visible = isChecked
row.Cells(i).Controls.OfType(Of DropDownList)().FirstOrDefault().ClearSelection()
row.Cells(i).Controls.OfType(Of DropDownList)().FirstOrDefault().Items.FindByValue(dr("Country").ToString()).Selected = True
End If
Next
End If
End If
Next
End If
End Sub
Screenshot