Transfer records from one Gridview to another Gridview Asp.net C# and VB.Net

Transfer records from one Listview to another Listview is a common task but sometimes client wants more which will be difficult to implement using Listview control. In that case developers may choose Gridview as an another option to achieve more flexibility with less effort. Here i am showing an example on “How one can Transfer or Shift data from one Gridview to another Gridview using Asp.net C# and VB.Net“. Have a look to the below output and then go for implementation:

Transfer_Record_One_Gridview_another_Gridview

To implement the example we need 2 Gridviews and 3 Buttons. So add the below code in your aspx page:

    <table>
        <tr>
            <td valign="top">
                <asp:GridView runat="server" ID="gvLeft" DataKeyNames="ID" AutoGenerateColumns="false" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">
                    <Columns>
                        <asp:TemplateField HeaderText="Select">
                            <ItemTemplate>
                                <asp:CheckBox runat="server" ID="chk"/>
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:BoundField DataField="Code" HeaderText="Supplier code"> </asp:BoundField>
                        <asp:BoundField DataField="Name" HeaderText="Supplier name"> </asp:BoundField>
                    </Columns>
                </asp:GridView>
            </td>

            <td>
                <asp:Button runat="server" ID="cmdRight" Text=">>>>" OnClick="cmdRight_Click"/><br />
                <asp:Button runat="server" ID="cmdLeft" Text="<<<<" OnClick="cmdLeft_Click"/>
            </td>

            <td valign="top">
                <asp:GridView runat="server" ID="gvRight" EmptyDataText="NO DATA FOUND" DataKeyNames="ID" HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White" CellPadding="5">
                    <Columns>
                        <asp:TemplateField HeaderText="Select">
                            <ItemTemplate>
                                <asp:CheckBox runat="server" ID="chk"/>
                            </ItemTemplate>
                        </asp:TemplateField>
                    
                        <asp:BoundField DataField="Code" HeaderText="Supplier code"> </asp:BoundField>
                        <asp:BoundField DataField="Name" HeaderText="Supplier name"> </asp:BoundField>
                    </Columns>
                </asp:GridView><br />
                <asp:Button runat="server" ID="cmdUpdate" Text="Update" OnClick="cmdUpdate_Click"/>
            </td>
        </tr>
    </table>

Now we need to populate left side Gridview first. You can populate data from database but for simplicity here i am using Datatable to provide “Paste and Run” facility. Don’t forget to add “using System.Data” namespace. Write the below codes under page load event:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtLeft = new DataTable("Supplier");

            dtLeft.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
            dtLeft.Columns.Add(new DataColumn("Code"));
            dtLeft.Columns.Add(new DataColumn("Name"));

            dtLeft.Rows.Add(1, "st0001", "S.R. Steel");
            dtLeft.Rows.Add(2, "ir0039", "Shadesh builders");
            dtLeft.Rows.Add(3, "cr0042", "Orchard confec.");
            dtLeft.Rows.Add(4, "er0078", "Windblow");
            dtLeft.Rows.Add(5, "bd0301", "Rahimkarim");

            gvLeft.DataSource = dtLeft;
            gvLeft.DataBind();

            DataTable dtRight = dtLeft.Clone();
            dtRight.Rows.Add(6, "qq0000", "I.j.k ALAM");

            gvRight.DataSource = dtRight;
            gvRight.DataBind();

            ViewState["dtLeft"] = dtLeft;
            ViewState["dtRight"] = dtRight;
        }
    }

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dtLeft As New DataTable

            dtLeft.Columns.Add(New DataColumn("ID", System.Type.GetType("System.UInt64")))
            dtLeft.Columns.Add(New DataColumn("Code"))
            dtLeft.Columns.Add(New DataColumn("Name"))

            dtLeft.Rows.Add(1, "st0001", "S.R. Steel")
            dtLeft.Rows.Add(2, "ir0039", "Shadesh builders")
            dtLeft.Rows.Add(3, "cr0042", "Orchard confec.")
            dtLeft.Rows.Add(4, "er0078", "Windblow")
            dtLeft.Rows.Add(5, "bd0301", "Rahimkarim")

            gvLeft.DataSource = dtLeft
            gvLeft.DataBind()

            Dim dtRight As New DataTable
            dtRight = dtLeft.Clone()
            dtRight.Rows.Add(6, "qq0000", "I.j.k ALAM")

            gvRight.DataSource = dtRight
            gvRight.DataBind()

            ViewState.Add("dtLeft", dtLeft)
            ViewState.Add("dtRight", dtRight)
        End If
    End Sub

Now we need to write code to response when user wants to transfer or shift data from one Gridview to another Gridview. To do that write the below server side method under right & left directional navigation button click event respectively:
C# Code:

    protected void cmdRight_Click(object sender, EventArgs e)
    {
        DataTable dtLeft = (DataTable)ViewState["dtLeft"];
        DataTable dtRight = (DataTable)ViewState["dtRight"];
        int tmp = 0;

        foreach (GridViewRow oItemLeft in gvLeft.Rows)
        {
            if (((CheckBox)oItemLeft.FindControl("chk")).Checked)
            {
                dtRight.Rows.Add(gvLeft.DataKeys[oItemLeft.DataItemIndex].Value, oItemLeft.Cells[1].Text, oItemLeft.Cells[2].Text);
                dtLeft.Rows.RemoveAt(oItemLeft.DataItemIndex - tmp);
                tmp++;
            }
        }

        gvRight.DataSource = dtRight;
        gvRight.DataBind();
        gvLeft.DataSource = dtLeft;
        gvLeft.DataBind();

        ViewState["dtLeft"] = dtLeft;
        ViewState["dtRight"] = dtRight;
    }

    protected void cmdLeft_Click(object sender, EventArgs e)
    {
        DataTable dtLeft = (DataTable)ViewState["dtLeft"];
        DataTable dtRight = (DataTable)ViewState["dtRight"];
        int tmp = 0;

        foreach (GridViewRow oItemRight in gvRight.Rows)
        {
            if (((CheckBox)oItemRight.FindControl("chk")).Checked)
            {
                dtLeft.Rows.Add(gvRight.DataKeys[oItemRight.DataItemIndex].Value, oItemRight.Cells[1].Text, oItemRight.Cells[2].Text);
                dtRight.Rows.RemoveAt(oItemRight.DataItemIndex - tmp);
                tmp++;
            }
        }

        gvRight.DataSource = dtRight;
        gvRight.DataBind();
        gvLeft.DataSource = dtLeft;
        gvLeft.DataBind();

        ViewState["dtLeft"] = dtLeft;
        ViewState["dtRight"] = dtRight;
    }

VB.Net Code:

    Protected Sub cmdRight_Click(sender As Object, e As System.EventArgs) Handles cmdRight.Click
        Dim dtLeft = CType(ViewState("dtLeft"), DataTable)
        Dim dtRight = CType(ViewState("dtRight"), DataTable)
        Dim tmp = 0

        For Each oItemLeft As GridViewRow In gvLeft.Rows
            If CType(oItemLeft.FindControl("chk"), CheckBox).Checked Then
                dtRight.Rows.Add(gvLeft.DataKeys(oItemLeft.DataItemIndex).Value, oItemLeft.Cells(1).Text, oItemLeft.Cells(2).Text)
                dtLeft.Rows.RemoveAt(oItemLeft.DataItemIndex - tmp)
                tmp = tmp + 1
            End If
        Next

        gvRight.DataSource = dtRight
        gvRight.DataBind()
        gvLeft.DataSource = dtLeft
        gvLeft.DataBind()

        ViewState.Add("dtLeft", dtLeft)
        ViewState.Add("dtRight", dtRight)
    End Sub

    Protected Sub cmdLeft_Click(sender As Object, e As System.EventArgs) Handles cmdLeft.Click
        Dim dtLeft = CType(ViewState("dtLeft"), DataTable)
        Dim dtRight = CType(ViewState("dtRight"), DataTable)
        Dim tmp = 0

        For Each oItemRight As GridViewRow In gvRight.Rows
            If CType(oItemRight.FindControl("chk"), CheckBox).Checked Then
                dtLeft.Rows.Add(gvLeft.DataKeys(oItemRight.DataItemIndex).Value, oItemRight.Cells(1).Text, oItemRight.Cells(2).Text)
                dtRight.Rows.RemoveAt(oItemRight.DataItemIndex - tmp)
                tmp = tmp + 1
            End If
        Next

        gvRight.DataSource = dtRight
        gvRight.DataBind()
        gvLeft.DataSource = dtLeft
        gvLeft.DataBind()

        ViewState.Add("dtLeft", dtLeft)
        ViewState.Add("dtRight", dtRight)
    End Sub

Now you can store the changes into database as per your requirement. To do that please follow my below sample code snippet under Update button:
C# Code:

    protected void cmdUpdate_Click(object sender, EventArgs e)
    {

        foreach (GridViewRow oItem in gvLeft.Rows)
        {
            string sID = gvLeft.DataKeys[oItem.DataItemIndex].Value.ToString();
            string sSQL = "UPDATE Supplier SET Status=0 WHERE ID=" + sID;
            //NOW RUN EXECUTENONQUERY METHOD OR WHATEVER YOU WANT. ID NOW AVAILABLE.
        }

        foreach (GridViewRow oItem in gvRight.Rows)
        {
            string sID = gvRight.DataKeys[oItem.DataItemIndex].Value.ToString();
            string sSQL = "UPDATE Supplier SET Status=1 WHERE ID=" + sID;
            //NOW RUN EXECUTENONQUERY METHOD OR WHATEVER YOU WANT. ID NOW AVAILABLE.
        }
    }

VB.Net Code:

    Protected Sub cmdUpdate_Click(sender As Object, e As System.EventArgs) Handles cmdUpdate.Click

        For Each oItem As GridViewRow In gvLeft.Rows
            Dim sID = gvLeft.DataKeys(oItem.DataItemIndex).Value.ToString()
            Dim sSQL = "UPDATE Supplier SET Status=0 WHERE ID=" + sID
            'NOW RUN EXECUTENONQUERY METHOD OR WHATEVER YOU WANT. ID NOW AVAILABLE.
        Next

        For Each oItem As GridViewRow In gvRight.Rows
            Dim sID = gvRight.DataKeys(oItem.DataItemIndex).Value.ToString()
            Dim sSQL = "UPDATE Supplier SET Status=0 WHERE ID=" + sID
            'NOW RUN EXECUTENONQUERY METHOD OR WHATEVER YOU WANT. ID NOW AVAILABLE.
        Next
    End Sub

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, Gridview, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

     

*