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.Move rows from one GridView to another GridView using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="ada20"> ada20</a>,</p> <p>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; DataKeyNames=&#34;CustomerId&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField&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;lblName&#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;Country&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblCountry&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Country&#34;) %&gt;&#39; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;GridView2&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; DataKeyNames=&#34;CustomerId&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;chk&#34; runat=&#34;server&#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;lblName&#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;Country&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Label1&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Country&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&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; 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">SqlConnection conString = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString); DataTable dtFirst, dtSecond; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); GridView2.DataSource = dtSecond; GridView2.DataBind(); } } public void BindGrid() { string query = &#34;SELECT * FROM Customers&#34;; SqlDataAdapter objDa = new SqlDataAdapter(query, conString); DataSet ds = new DataSet(); objDa.Fill(ds); if (ds.Tables[0].Rows.Count &gt; 0) { GridView1.DataSource = ds; GridView1.DataBind(); } dtFirst = ds.Tables[0]; dtSecond = dtFirst.Clone(); ViewState[&#34;dtFirst&#34;] = dtFirst; ViewState[&#34;dtSecond&#34;] = dtSecond; } protected void Transfer(object sender, EventArgs e) { dtFirst = (DataTable)ViewState[&#34;dtFirst&#34;]; dtSecond = (DataTable)ViewState[&#34;dtSecond&#34;]; int i = 0; foreach (GridViewRow row in GridView1.Rows) { if (((CheckBox)row.FindControl(&#34;chk&#34;)).Checked) { dtSecond.Rows.Add(GridView1.DataKeys[row.DataItemIndex].Value, ((Label)row.FindControl(&#34;lblName&#34;)).Text, ((Label)row.FindControl(&#34;lblCountry&#34;)).Text); dtFirst.Rows.RemoveAt(row.DataItemIndex - i); i++; } } GridView2.DataSource = dtSecond; GridView2.DataBind(); GridView1.DataSource = dtFirst; GridView1.DataBind(); ViewState[&#34;dtFirst&#34;] = dtFirst; ViewState[&#34;dtSecond&#34;] = dtSecond; }</pre> <p><strong>VB.Net</strong> </p> <pre class="brush: vb">Private conString As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString) Private dtFirst, dtSecond As DataTable Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then BindGrid() GridView2.DataSource = dtSecond GridView2.DataBind() End If End Sub Public Sub BindGrid() Dim query As String = &#34;SELECT * FROM Customers&#34; Dim objDa As SqlDataAdapter = New SqlDataAdapter(query, conString) Dim ds As DataSet = New DataSet() objDa.Fill(ds) If ds.Tables(0).Rows.Count &gt; 0 Then GridView1.DataSource = ds GridView1.DataBind() End If dtFirst = ds.Tables(0) dtSecond = dtFirst.Clone() ViewState(&#34;dtFirst&#34;) = dtFirst ViewState(&#34;dtSecond&#34;) = dtSecond End Sub Protected Sub Transfer(ByVal sender As Object, ByVal e As EventArgs) dtFirst = CType(ViewState(&#34;dtFirst&#34;), DataTable) dtSecond = CType(ViewState(&#34;dtSecond&#34;), DataTable) Dim i As Integer = 0 For Each row As GridViewRow In GridView1.Rows If (CType(row.FindControl(&#34;chk&#34;), CheckBox)).Checked Then dtSecond.Rows.Add(GridView1.DataKeys(row.DataItemIndex).Value, (CType(row.FindControl(&#34;lblName&#34;), Label)).Text, (CType(row.FindControl(&#34;lblCountry&#34;), Label)).Text) dtFirst.Rows.RemoveAt(row.DataItemIndex - i) i += 1 End If Next GridView2.DataSource = dtSecond GridView2.DataBind() GridView1.DataSource = dtFirst GridView1.DataBind() ViewState(&#34;dtFirst&#34;) = dtFirst ViewState(&#34;dtSecond&#34;) = dtSecond End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/DKjPyqH.gif" alt="" width="248" height="342" /></p>https://www.aspforums.net:443/Threads/183229/Move-rows-from-one-GridView-to-another-GridView-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/183229/Move-rows-from-one-GridView-to-another-GridView-using-C-and-VBNet-in-ASPNet/Tue, 28 May 2019 01:56:56 GMT