How To Move Selected Record From One GridView to another

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MoveRecord.aspx.cs" Inherits="MoveRecord" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gridView1" runat="server">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSelect" runat="server" />
                        <asp:HiddenField ID="hdValue" runat="server" Value='<%#Eval("ID") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    <div>
        <asp:Button ID="btnMove" runat="server" Text="Move" OnClick="btnMove_Click" />
    </div>
    <div>
        <asp:GridView ID="gridView2" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class MoveRecord : System.Web.UI.Page
{
    const string key = "MyDataSource";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }

    }
    private void BindGridView()
    {
        if (Session[key] == null)
        {
            gridView1.DataSource = GetDataSource();
            gridView1.DataBind();
        }
        else
        {
            gridView1.DataSource = (DataTable)Session[key];
            gridView1.DataBind();
        }

    }
    protected DataTable GetDataSource()
    {
        try
        {
            DataTable dt = new DataTable();
            dt = new DataTable();
            dt.Columns.Add("ID", typeof(int)).AutoIncrement = true;
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Age", typeof(int));
            DataColumn[] keys = new DataColumn[2];
            keys[0] = dt.Columns["ID"];
            dt.PrimaryKey = keys;
            dt.Rows.Add("1", "first object", 34);
            dt.Rows.Add("2", "second object", 24);
            dt.Rows.Add("3", "third object", 34);
            dt.Rows.Add("4", "fourth object", 24);
            dt.Rows.Add("5", "fifth object", 34);
       
            Session[key] = dt;
            return dt;
        }
        catch
        {
            return null;
        }
    }
    protected void btnMove_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dtMain = Session[key] as DataTable;
            //copy the schema of source table
            DataTable dtClone = dtMain.Clone();
            foreach (GridViewRow gv in gridView1.Rows)
            {
                CheckBox chk = gv.FindControl("chkSelect") as CheckBox;
                HiddenField hdValue = gv.FindControl("hdValue") as HiddenField;
                if (chk.Checked)
                {
                    //get only the rows you want
                    DataRow[] results = dtMain.Select("ID=" + hdValue.Value + "");
                    //populate new destination table
                    foreach (DataRow dr in results)
                    {
                        dtClone.ImportRow(dr);
                    }
                }
                gridView2.DataSource = dtClone;
                gridView2.DataBind();
            }
        }
        catch
        {
            BindGridView();
        }

    }
}

15 comments:

  1. Thanks...it works like wonder

    ReplyDelete
  2. Hi I triwed the above however my second gridview isn t coming out c an I post the code to see if you can help?

    ReplyDelete
  3. Hi,
    Thanks... Excellent work... Very helpful...

    ReplyDelete
  4. Nice one,can you tell me how to move grid view selected row move from grid to xml file?

    ReplyDelete
  5. i want this same process using sql database..
    can u help..

    ReplyDelete
  6. @Karthic:Just fill the dataset from database and rest will be same

    ReplyDelete
  7. i was try but i can't do it. my project is hotel management. in this grid process is very useful in food ordering. i created database (fields : Item, Variety, Price)connect to gridview1 successfully. but it can't display in gridview2. pls explain the process.

    ReplyDelete
  8. then also explain how to declare the dataset. i dnt knw any idea.. sory for the dstrb.. bcze tis s main process in my pjt.. thk u..

    ReplyDelete
  9. @Karthick:send me your email address

    ReplyDelete
  10. hai sir.. your coding is working in two tier perfectly.. but our company said, doing only on three tier architecture.. i was try it.. can i send the coding.. pls verify it..

    ReplyDelete
  11. gud post....
    thanks a ton yaar. it helps me.

    ReplyDelete