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 (Pass) Merged and checked (selected) GridView Rows to another GridView in ASP.Net<p>Hey <a class="username" rel="dorsa"> dorsa</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; OnDataBound=&#34;OnDataBound&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;ContactName&#34; HeaderText=&#34;ContactName&#34; ItemStyle-Width=&#34;150&#34; SortExpression=&#34;ContactName&#34;&gt; &lt;HeaderStyle CssClass=&#34;header-center&#34;&gt;&lt;/HeaderStyle&gt; &lt;ItemStyle Width=&#34;150px&#34;&gt;&lt;/ItemStyle&gt; &lt;/asp:BoundField&gt; &lt;asp:BoundField DataField=&#34;City&#34; HeaderText=&#34;City&#34; ItemStyle-Width=&#34;150&#34; SortExpression=&#34;City&#34;&gt; &lt;HeaderStyle CssClass=&#34;header-center&#34;&gt;&lt;/HeaderStyle&gt; &lt;ItemStyle Width=&#34;150px&#34;&gt;&lt;/ItemStyle&gt; &lt;/asp:BoundField&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; ItemStyle-Width=&#34;150&#34; SortExpression=&#34;Country&#34;&gt; &lt;HeaderStyle CssClass=&#34;header-center&#34;&gt;&lt;/HeaderStyle&gt; &lt;ItemStyle Width=&#34;150px&#34;&gt;&lt;/ItemStyle&gt; &lt;/asp:BoundField&gt; &lt;asp:TemplateField HeaderText=&#34;select&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;chkSelect&#34; runat=&#34;server&#34; AutoPostBack=&#34;true&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;HeaderStyle BackColor=&#34;#3AC0F2&#34; ForeColor=&#34;White&#34;&gt;&lt;/HeaderStyle&gt; &lt;/asp:GridView&gt; &lt;asp:Button Text=&#34;Transfer&#34; runat=&#34;server&#34; OnClick=&#34;Transfer&#34; /&gt; &lt;br /&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;GridView2&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;ContactName&#34; HeaderText=&#34;ContactName&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; /&gt; &lt;asp:BoundField DataField=&#34;City&#34; HeaderText=&#34;City&#34; /&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; using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); GridView1.DataSource = GetData(&#34;SELECT TOP 5 ContactName, Country, City FROM Customers WHERE Country IS NOT NULL GROUP BY Country, City, ContactName&#34;); GridView1.DataBind(); } } private DataTable GetData(string query) { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; sda.SelectCommand = cmd; sda.Fill(dt); ViewState[&#34;dt&#34;] = dt; } } return dt; } } protected void OnDataBound(object sender, EventArgs e) { for (int i = GridView1.Rows.Count - 1; i &gt; 0; i--) { GridViewRow row = GridView1.Rows[i]; GridViewRow previousRow = GridView1.Rows[i - 1]; CheckBox chk = row.FindControl(&#34;chkSelect&#34;) as CheckBox; int count = row.Cells.Count - 1; for (int j = 0; j &lt; row.Cells.Count - 1; j++) { if (row.Cells[j].Text == previousRow.Cells[j].Text) { if (previousRow.Cells[j].RowSpan == 0) { if (row.Cells[j].RowSpan == 0) { previousRow.Cells[j].RowSpan += 2; previousRow.Cells[count].RowSpan = 2; } else { previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1; previousRow.Cells[count].RowSpan = row.Cells[j].RowSpan + 1; } chk.Visible = false; row.Cells[j].Visible = false; row.Cells[j + 1].Visible = false; } } } } } protected void Transfer(object sender, EventArgs e) { DataTable dt = ViewState[&#34;dt&#34;] as DataTable; DataTable dtChecked = dt.Clone(); foreach (GridViewRow row in GridView1.Rows) { CheckBox chk = row.FindControl(&#34;chkSelect&#34;) as CheckBox; if (chk.Checked) { DataRow[] dr = dt.Select(&#34;Country=&#39;&#34; + row.Cells[2].Text + &#34;&#39;&#34;); dtChecked.Merge(dr.CopyToDataTable()); } } GridView2.DataSource = dtChecked; GridView2.DataBind(); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim dt As DataTable = New DataTable() GridView1.DataSource = GetData(&#34;SELECT TOP 5 ContactName, Country, City FROM Customers WHERE Country IS NOT NULL GROUP BY Country, City, ContactName&#34;) GridView1.DataBind() End If End Sub Private Function GetData(ByVal query As String) As DataTable Dim dt As DataTable = New DataTable() Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(query) Using sda As SqlDataAdapter = New SqlDataAdapter() cmd.CommandType = CommandType.Text cmd.Connection = con sda.SelectCommand = cmd sda.Fill(dt) ViewState(&#34;dt&#34;) = dt End Using End Using Return dt End Using End Function Protected Sub OnDataBound(sender As Object, e As EventArgs) For i As Integer = GridView1.Rows.Count - 1 To 1 Step -1 Dim row As GridViewRow = GridView1.Rows(i) Dim chk As CheckBox = TryCast(row.FindControl(&#34;chkSelect&#34;), CheckBox) Dim count As Integer = row.Cells.Count - 1 Dim previousRow As GridViewRow = GridView1.Rows(i - 1) For j As Integer = 0 To row.Cells.Count - 2 If row.Cells(j).Text = previousRow.Cells(j).Text Then If previousRow.Cells(j).RowSpan = 0 Then If row.Cells(j).RowSpan = 0 Then previousRow.Cells(j).RowSpan += 2 previousRow.Cells(count).RowSpan = 2 Else previousRow.Cells(j).RowSpan = row.Cells(j).RowSpan + 1 previousRow.Cells(count).RowSpan = row.Cells(count).RowSpan + 1 End If chk.Visible = False row.Cells(j).Visible = False row.Cells(count).Visible = False End If End If Next Next End Sub Protected Sub Transfer(ByVal sender As Object, ByVal e As EventArgs) Dim dt As DataTable = TryCast(ViewState(&#34;dt&#34;), DataTable) Dim dtChecked As DataTable = dt.Clone() For Each row As GridViewRow In GridView1.Rows Dim chk As CheckBox = TryCast(row.FindControl(&#34;chkSelect&#34;), CheckBox) If chk.Checked Then Dim dr As DataRow() = dt.Select(&#34;Country=&#39;&#34; &amp; row.Cells(2).Text &amp; &#34;&#39;&#34;) dtChecked.Merge(dr.CopyToDataTable()) End If Next GridView2.DataSource = dtChecked GridView2.DataBind() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/ZGfzlh3.gif" alt="" width="354" height="454" /></p>https://www.aspforums.net:443/Threads/110479/Transfer-Pass-Merged-and-checked-selected-GridView-Rows-to-another-GridView-in-ASPNet/https://www.aspforums.net:443/Threads/110479/Transfer-Pass-Merged-and-checked-selected-GridView-Rows-to-another-GridView-in-ASPNet/Mon, 31 Dec 2018 03:45:18 GMT