Move rows from one GridView to another GridView using C# and VB.Net in ASP.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

Hello to all,

I tried this example:

 https://www.aspsnippets.com/Articles/Implement-CheckChanged-event-of-CheckBox-inside-GridView-in-ASPNet.aspx

But, I want to move the selected row into another gridview, remove it from the first and save the row to the new gridview, using database. In addition,I declare 2 columns because I have 2 options  for each row as bit type(0/1).

(I develop in Visual Studio 2017, ASP.Net Web form, C#).

So, which is the additional code for my case?

Thank you.

 

 

Posted 4 months ago

Hi ada20,

Refer below sample.

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerId">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chk" runat="server" AutoPostBack="true" OnCheckedChanged="Transfer" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Country">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<br />
<br />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" DataKeyNames="CustomerId">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chk" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Country">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

SqlConnection conString = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].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 = "SELECT * FROM Customers";
    SqlDataAdapter objDa = new SqlDataAdapter(query, conString);
    DataSet ds = new DataSet();
    objDa.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    dtFirst = ds.Tables[0];
    dtSecond = dtFirst.Clone();
    ViewState["dtFirst"] = dtFirst;
    ViewState["dtSecond"] = dtSecond;
}

protected void Transfer(object sender, EventArgs e)
{
    dtFirst = (DataTable)ViewState["dtFirst"];
    dtSecond = (DataTable)ViewState["dtSecond"];
    int i = 0;
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (((CheckBox)row.FindControl("chk")).Checked)
        {
            dtSecond.Rows.Add(GridView1.DataKeys[row.DataItemIndex].Value, ((Label)row.FindControl("lblName")).Text, ((Label)row.FindControl("lblCountry")).Text);
            dtFirst.Rows.RemoveAt(row.DataItemIndex - i);
            i++;
        }
    }
    GridView2.DataSource = dtSecond;
    GridView2.DataBind();
    GridView1.DataSource = dtFirst;
    GridView1.DataBind();
    ViewState["dtFirst"] = dtFirst;
    ViewState["dtSecond"] = dtSecond;
}

VB.Net

Private conString As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").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 = "SELECT * FROM Customers"
    Dim objDa As SqlDataAdapter = New SqlDataAdapter(query, conString)
    Dim ds As DataSet = New DataSet()
    objDa.Fill(ds)

    If ds.Tables(0).Rows.Count > 0 Then
        GridView1.DataSource = ds
        GridView1.DataBind()
    End If

    dtFirst = ds.Tables(0)
    dtSecond = dtFirst.Clone()
    ViewState("dtFirst") = dtFirst
    ViewState("dtSecond") = dtSecond
End Sub

Protected Sub Transfer(ByVal sender As Object, ByVal e As EventArgs)
    dtFirst = CType(ViewState("dtFirst"), DataTable)
    dtSecond = CType(ViewState("dtSecond"), DataTable)
    Dim i As Integer = 0

    For Each row As GridViewRow In GridView1.Rows

        If (CType(row.FindControl("chk"), CheckBox)).Checked Then
            dtSecond.Rows.Add(GridView1.DataKeys(row.DataItemIndex).Value, (CType(row.FindControl("lblName"), Label)).Text, (CType(row.FindControl("lblCountry"), Label)).Text)
            dtFirst.Rows.RemoveAt(row.DataItemIndex - i)
            i += 1
        End If
    Next

    GridView2.DataSource = dtSecond
    GridView2.DataBind()
    GridView1.DataSource = dtFirst
    GridView1.DataBind()
    ViewState("dtFirst") = dtFirst
    ViewState("dtSecond") = dtSecond
End Sub

Screenshot