Hey Alauddin,
Please refer below sample.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"
AllowPaging="true" PageSize="2" DataKeyNames="CustomerId" OnPageIndexChanging="gvCustomers_PageIndexChanging"
OnPageIndexChanged="gvCustomers_PageIndexChanged">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Display" OnClick="Update" />
<asp:Label ID="lblMessage" runat="server" />
<asp:GridView runat="server" ID="gvCheckedCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
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();
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;
string name = string.Empty;
string country = string.Empty;
if (dt != null)
{
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)
{
name += row.Cells[1].Controls.OfType<Label>().FirstOrDefault().Text;
country += row.Cells[2].Controls.OfType<Label>().FirstOrDefault().Text;
int id = Convert.ToInt32(gvCustomers.DataKeys[row.RowIndex].Value);
dt.Rows.Add(id, name, country);
}
}
}
gvCustomers.PageIndex = 0;
this.BindGrid();
ViewState["UpdatedRecord"] = null;
gvCheckedCustomers.DataSource = dt;
gvCheckedCustomers.DataBind();
lblMessage.Text = "Count is : " + dt.Rows.Count.ToString();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlCommand cmd = new SqlCommand("SELECT DISTINCT(Country) FROM CustomerTest11");
string country = (e.Row.FindControl("lblCountry") as Label).Text;
}
}
protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvCustomers.PageIndex = e.NewPageIndex;
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<Label>().FirstOrDefault().Text;
string country = row.Cells[2].Controls.OfType<Label>().FirstOrDefault().Text;
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<Label>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = isChecked;
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Text = dr["Name"].ToString();
}
if (row.Cells[i].Controls.OfType<Label>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = isChecked;
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Text = dr["Name"].ToString();
}
}
}
}
}
}
}
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)
Dim name As String = String.Empty
Dim country As String = String.Empty
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
name += row.Cells(1).Controls.OfType(Of Label)().FirstOrDefault().Text
country += row.Cells(2).Controls.OfType(Of Label)().FirstOrDefault().Text
Dim id As Integer = Convert.ToInt32(gvCustomers.DataKeys(row.RowIndex).Value)
dt.Rows.Add(id, name, country)
End If
Next
End If
gvCustomers.PageIndex = 0
Me.BindGrid()
ViewState("UpdatedRecord") = Nothing
gvCheckedCustomers.DataSource = dt
gvCheckedCustomers.DataBind()
lblMessage.Text = "Count is : " & dt.Rows.Count.ToString()
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 country As String = (TryCast(e.Row.FindControl("lblCountry"), Label)).Text
End If
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 Label)().FirstOrDefault().Text
Dim country As String = row.Cells(2).Controls.OfType(Of Label)().FirstOrDefault().Text
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 Label)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Visible = isChecked
row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Text = dr("Name").ToString()
End If
If row.Cells(i).Controls.OfType(Of Label)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Visible = isChecked
row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Text = dr("Name").ToString()
End If
Next
End If
End If
Next
End If
End Sub
Screenshot
