Hi akhter,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
</Columns>
</asp:GridView>
<hr />
<asp:Button Text="Add" runat="server" OnClick="OnAdd" />
<asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton>
<cc1:ModalPopupExtender ID="ModalPopupExtender1" BehaviorID="mpe" runat="server"
PopupControlID="pnlPopup" TargetControlID="lnkDummy" BackgroundCssClass="modalBackground"
CancelControlID="btnHide">
</cc1:ModalPopupExtender>
<asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none">
<div class="header">
Modal Popup
</div>
<div class="body">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnSend" Text="Send" runat="server" OnClick="SendRowData" />
<asp:Button ID="btnHide" runat="server" Text="Close" />
</div>
</asp:Panel>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["SelectedData"] != null)
{
DataTable selectedData = (DataTable)Session["SelectedData"];
selectedData.Columns.RemoveAt(0);
selectedData.Merge((DataTable)Session["Data"]);
Session["Data"] = selectedData.AsEnumerable().OrderBy(x => x["CustomerID"]).CopyToDataTable();
ModalPopupExtender1.Hide();
}
BindGrid();
}
}
protected void OnAdd(object sender, EventArgs e)
{
ModalPopupExtender1.Show();
BindPopupGrid();
}
protected void SendRowData(object sender, EventArgs e)
{
DataTable table = new DataTable();
GridViewRow headerRow = GridView1.HeaderRow;
foreach (TableCell item in headerRow.Cells)
{
DataColumn column = new DataColumn();
column.ColumnName = item.Text;
table.Columns.Add(column);
}
foreach (GridViewRow rows in GridView1.Rows)
{
if (((RadioButton)rows.FindControl("RadioButton1")).Checked)
{
DataRow row = table.NewRow();
int i = 0;
foreach (TableCell item in rows.Cells)
{
row[i] = item.Text;
i++;
}
table.Rows.Add(row);
}
}
Session["SelectedData"] = table;
Response.Redirect(Request.Url.AbsoluteUri);
}
private void BindGrid()
{
if (Session["Data"] == null)
{
string query = "SELECT TOP 5 CustomerID,City,PostalCode FROM Customers";
Session["Data"] = GetData(query);
}
GridView2.DataSource = Session["Data"];
GridView2.DataBind();
}
private void BindPopupGrid()
{
DataTable dt = GetData("SELECT TOP 5 CustomerID,City,PostalCode FROM Customers ORDER BY CustomerID DESC");
GridView1.DataSource = dt;
GridView1.DataBind();
}
private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
If Session("SelectedData") IsNot Nothing Then
Dim selectedData As DataTable = CType(Session("SelectedData"), DataTable)
selectedData.Columns.RemoveAt(0)
selectedData.Merge(CType(Session("Data"), DataTable))
Session("Data") = selectedData.AsEnumerable().OrderBy(Function(x) x("CustomerID")).CopyToDataTable()
ModalPopupExtender1.Hide()
End If
BindGrid()
End If
End Sub
Protected Sub OnAdd(ByVal sender As Object, ByVal e As EventArgs)
ModalPopupExtender1.Show()
BindPopupGrid()
End Sub
Protected Sub SendRowData(ByVal sender As Object, ByVal e As EventArgs)
Dim table As DataTable = New DataTable()
Dim headerRow As GridViewRow = GridView1.HeaderRow
For Each item As TableCell In headerRow.Cells
Dim column As DataColumn = New DataColumn()
column.ColumnName = item.Text
table.Columns.Add(column)
Next
For Each rows As GridViewRow In GridView1.Rows
If (CType(rows.FindControl("RadioButton1"), RadioButton)).Checked Then
Dim row As DataRow = table.NewRow()
Dim i As Integer = 0
For Each item As TableCell In rows.Cells
row(i) = item.Text
i += 1
Next
table.Rows.Add(row)
End If
Next
Session("SelectedData") = table
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Private Sub BindGrid()
If Session("Data") Is Nothing Then
Dim query As String = "SELECT TOP 5 CustomerID,City,PostalCode FROM Customers"
Session("Data") = GetData(query)
End If
GridView2.DataSource = Session("Data")
GridView2.DataBind()
End Sub
Private Sub BindPopupGrid()
Dim dt As DataTable = GetData("SELECT TOP 5 CustomerID,City,PostalCode FROM Customers ORDER BY CustomerID DESC")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Private Function GetData(ByVal query As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot
![](https://i.imgur.com/NhkRs4d.gif)