ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Transfer ASP.Net GridView Selected Rows from one page to another using CheckBox in C# and VB.Net<p>Hi <a class="username" rel="VinitaAkha">VinitaAkha</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <p><strong>CS.aspx</strong></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#39;[id*=chk]&#39;).on(&#39;change&#39;, function () { var value = 0; $(&#39;[id*=chk]:checked&#39;).each(function () { var row = $(this).closest(&#39;tr&#39;); value = value + parseInt(row.find(&#39;[id*=Total]&#39;).html()); }); $(&#34;[id*=gvData] [id*=lblGrandTotal]&#34;).html(value); $(&#34;[id*=gvSelected] [id*=lblGrandTotal]&#34;).html(value); }); }); &lt;/script&gt; &lt;asp:GridView ID=&#34;gvData&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; ShowFooter=&#34;true&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;Select&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;chk&#34; runat=&#34;server&#34; AutoPostBack=&#34;true&#34; OnCheckedChanged=&#34;Transfer&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Name&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Name&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Name&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Email&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Email&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Email&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;State&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;State&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;State&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;District&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;District&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;District&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Address&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Address&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Address&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Contact&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Contact&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Contact&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;PQuantity&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;PQuantity&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;PQuantity&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Quantity&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Quantity&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Quantity&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Total&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Total&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Total&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; Total : &lt;asp:Label ID=&#34;lblGrandTotal&#34; runat=&#34;server&#34; /&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><strong>Default.aspx</strong></p> <pre class="brush: html">&lt;u&gt;Selected Rows&lt;/u&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;gvSelected&#34; runat=&#34;server&#34; ShowFooter=&#34;true&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;State&#34; HeaderText=&#34;State&#34; /&gt; &lt;asp:BoundField DataField=&#34;District&#34; HeaderText=&#34;District&#34; /&gt; &lt;asp:BoundField DataField=&#34;Address&#34; HeaderText=&#34;Address&#34; /&gt; &lt;asp:BoundField DataField=&#34;Email&#34; HeaderText=&#34;Email&#34; /&gt; &lt;asp:BoundField DataField=&#34;Contact&#34; HeaderText=&#34;Contact&#34; /&gt; &lt;asp:BoundField DataField=&#34;PQuantity&#34; HeaderText=&#34;PQuantity&#34; /&gt; &lt;asp:BoundField DataField=&#34;Quantity&#34; HeaderText=&#34;Quantity&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;Total&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Total&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Total&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; Total : &lt;asp:Label ID=&#34;lblGrandTotal&#34; runat=&#34;server&#34; /&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <p><strong>CS.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[10] { new DataColumn(&#34;Role&#34;, typeof(string)), new DataColumn(&#34;Email&#34;, typeof(string)), new DataColumn(&#34;Contact&#34;,typeof(string)), new DataColumn(&#34;State&#34;, typeof(string)), new DataColumn(&#34;District&#34;, typeof(string)), new DataColumn(&#34;Address&#34;,typeof(string)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Quantity&#34;, typeof(int)), new DataColumn(&#34;PQuantity&#34;,typeof(int)), new DataColumn(&#34;Total&#34;,typeof(int))}); dt.Rows.Add(&#34;Role 1&#34;, &#34;Email 1&#34;, &#34;Contact 1&#34;, &#34;State 1&#34;, &#34;District 1&#34;, &#34;Address 1&#34;, &#34;Name 1&#34;, 10, 5, 50); dt.Rows.Add(&#34;Role 2&#34;, &#34;Email 2&#34;, &#34;Contact 2&#34;, &#34;State 2&#34;, &#34;District 2&#34;, &#34;Address 2&#34;, &#34;Name 2&#34;, 20, 5, 100); dt.Rows.Add(&#34;Role 3&#34;, &#34;Email 3&#34;, &#34;Contact 3&#34;, &#34;State 3&#34;, &#34;District 3&#34;, &#34;Address 3&#34;, &#34;Name 3&#34;, 15, 5, 75); dt.Rows.Add(&#34;Role 4&#34;, &#34;Email 4&#34;, &#34;Contact 4&#34;, &#34;State 4&#34;, &#34;District 4&#34;, &#34;Address 4&#34;, &#34;Name 4&#34;, 12, 5, 60); gvData.DataSource = dt; gvData.DataBind(); } } protected void Transfer(object sender, EventArgs e) { decimal total = 0; try { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[9] { new DataColumn(&#34;Name&#34;), new DataColumn(&#34;Quantity&#34;), new DataColumn(&#34;Contact&#34;), new DataColumn(&#34;Email&#34;), new DataColumn(&#34;State&#34;), new DataColumn(&#34;District&#34;), new DataColumn(&#34;Address&#34;), new DataColumn(&#34;PQuantity&#34;), new DataColumn(&#34;Total&#34;) }); foreach (GridViewRow row in gvData.Rows) { if (row.RowType == DataControlRowType.DataRow) { CheckBox chkRow = (row.Cells[0].FindControl(&#34;chk&#34;) as CheckBox); if (chkRow.Checked) { string Name = (row.Cells[1].FindControl(&#34;Name&#34;) as Label).Text; string Quantity = (row.Cells[2].FindControl(&#34;Quantity&#34;) as Label).Text; string Contact = (row.Cells[3].FindControl(&#34;Contact&#34;) as Label).Text; string Email = (row.Cells[4].FindControl(&#34;Email&#34;) as Label).Text; string State = (row.Cells[5].FindControl(&#34;State&#34;) as Label).Text; string District = (row.Cells[6].FindControl(&#34;District&#34;) as Label).Text; string Address = (row.Cells[7].FindControl(&#34;Address&#34;) as Label).Text; string PQuantity = (row.Cells[8].FindControl(&#34;PQuantity&#34;) as Label).Text; string Total = (row.Cells[9].FindControl(&#34;Total&#34;) as Label).Text; total += Convert.ToDecimal(Total); dt.Rows.Add(Name, Quantity, Contact, Email, State, District, Address, PQuantity, Total); } } } Session[&#34;dt&#34;] = dt; Session[&#34;Tatal&#34;] = total; Response.Redirect(&#34;Default.aspx&#34;); } catch (Exception ex) { Response.Write(ex); } }</pre> <p><strong>Default.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = Session[&#34;dt&#34;] as DataTable; gvSelected.DataSource = dt; gvSelected.DataBind(); (gvSelected.FooterRow.FindControl(&#34;lblGrandTotal&#34;) as Label).Text = Session[&#34;Tatal&#34;].ToString(); } }</pre> <p><strong>VB.Net</strong></p> <p><strong>VB.aspx.vb</strong></p> <pre class="brush: vb">Class SurroundingClass Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load If Not Me.IsPostBack Then Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn(9) {New DataColumn(&#34;Role&#34;, GetType(String)), New DataColumn(&#34;Email&#34;, GetType(String)), New DataColumn(&#34;Contact&#34;, GetType(String)), New DataColumn(&#34;State&#34;, GetType(String)), New DataColumn(&#34;District&#34;, GetType(String)), New DataColumn(&#34;Address&#34;, GetType(String)), New DataColumn(&#34;Name&#34;, GetType(String)), New DataColumn(&#34;Quantity&#34;, GetType(Integer)), New DataColumn(&#34;PQuantity&#34;, GetType(Integer)), New DataColumn(&#34;Total&#34;, GetType(Integer))}) dt.Rows.Add(&#34;Role 1&#34;, &#34;Email 1&#34;, &#34;Contact 1&#34;, &#34;State 1&#34;, &#34;District 1&#34;, &#34;Address 1&#34;, &#34;Name 1&#34;, 10, 5, 50) dt.Rows.Add(&#34;Role 2&#34;, &#34;Email 2&#34;, &#34;Contact 2&#34;, &#34;State 2&#34;, &#34;District 2&#34;, &#34;Address 2&#34;, &#34;Name 2&#34;, 20, 5, 100) dt.Rows.Add(&#34;Role 3&#34;, &#34;Email 3&#34;, &#34;Contact 3&#34;, &#34;State 3&#34;, &#34;District 3&#34;, &#34;Address 3&#34;, &#34;Name 3&#34;, 15, 5, 75) dt.Rows.Add(&#34;Role 4&#34;, &#34;Email 4&#34;, &#34;Contact 4&#34;, &#34;State 4&#34;, &#34;District 4&#34;, &#34;Address 4&#34;, &#34;Name 4&#34;, 12, 5, 60) gvData.DataSource = dt gvData.DataBind() End If End Sub Protected Sub Transfer(ByVal sender As Object, ByVal e As EventArgs) Dim total As Decimal = 0 Try Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn(8) {New DataColumn(&#34;Name&#34;), New DataColumn(&#34;Quantity&#34;), New DataColumn(&#34;Contact&#34;), New DataColumn(&#34;Email&#34;), New DataColumn(&#34;State&#34;), New DataColumn(&#34;District&#34;), New DataColumn(&#34;Address&#34;), New DataColumn(&#34;PQuantity&#34;), New DataColumn(&#34;Total&#34;)}) For Each row As GridViewRow In gvData.Rows If row.RowType = DataControlRowType.DataRow Then Dim chkRow As CheckBox = (TryCast(row.Cells(0).FindControl(&#34;chk&#34;), CheckBox)) If chkRow.Checked Then Dim Name As String = (TryCast(row.Cells(1).FindControl(&#34;Name&#34;), Label)).Text Dim Quantity As String = (TryCast(row.Cells(2).FindControl(&#34;Quantity&#34;), Label)).Text Dim Contact As String = (TryCast(row.Cells(3).FindControl(&#34;Contact&#34;), Label)).Text Dim Email As String = (TryCast(row.Cells(4).FindControl(&#34;Email&#34;), Label)).Text Dim State As String = (TryCast(row.Cells(5).FindControl(&#34;State&#34;), Label)).Text Dim District As String = (TryCast(row.Cells(6).FindControl(&#34;District&#34;), Label)).Text Dim Address As String = (TryCast(row.Cells(7).FindControl(&#34;Address&#34;), Label)).Text Dim PQuantity As String = (TryCast(row.Cells(8).FindControl(&#34;PQuantity&#34;), Label)).Text Dim Total As String = (TryCast(row.Cells(9).FindControl(&#34;Total&#34;), Label)).Text total += Convert.ToDecimal(Total) dt.Rows.Add(Name, Quantity, Contact, Email, State, District, Address, PQuantity, Total) End If End If Next Session(&#34;dt&#34;) = dt Session(&#34;Tatal&#34;) = total Response.Redirect(&#34;Default.aspx&#34;) Catch ex As Exception Response.Write(ex) End Try End Sub End Class</pre> <p><strong>DefaultVB.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load If Not Me.IsPostBack Then Dim dt As DataTable = TryCast(Session(&#34;dt&#34;), DataTable) gvSelected.DataSource = dt gvSelected.DataBind() (TryCast(gvSelected.FooterRow.FindControl(&#34;lblGrandTotal&#34;), Label)).Text = Session(&#34;Tatal&#34;).ToString() End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/E5OMUsw.gif" alt="" width="557" height="260" /></p>https://www.aspforums.net:443/Threads/813201/Transfer-ASPNet-GridView-Selected-Rows-from-one-page-to-another-using-CheckBox-in-C-and-VBNet/https://www.aspforums.net:443/Threads/813201/Transfer-ASPNet-GridView-Selected-Rows-from-one-page-to-another-using-CheckBox-in-C-and-VBNet/Wed, 07 Aug 2019 05:45:04 GMT