Hi narasiman,
Please refer below sample.
Database
For this sample I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:ScriptManager ID="src1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updCustomers" runat="server">
<ContentTemplate>
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chk1" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk2" runat="server" AutoPostBack="true" OnCheckedChanged="gvCustomers_SelectedIndexChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
ViewState["dt"] = dt;
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
protected void gvCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
if (gvCustomers.Rows.Count > 0)
{
int index = ((sender as CheckBox).NamingContainer as GridViewRow).RowIndex;
DataTable dt = ViewState["dt"] as DataTable;
DataTable dtUpdated = dt.Clone();
dtUpdated.Rows.Add(dt.Rows[index].ItemArray);
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i != index)
{
dtUpdated.Rows.Add(dt.Rows[i].ItemArray);
}
}
ViewState["dt"] = dtUpdated;
gvCustomers.DataSource = dtUpdated;
gvCustomers.DataBind();
}
}
Screenshot