Thursday 1 August 2013

Move or Transfer Selected Gridview Row to another Gridview in Asp.Net


Description:-



in this Example we Explain that How to Export or Move one Gridview Row to another Gridview Based on Checkbox Selection.

In this Example We Define to Gridview in which First GridView only Conatin one Coloumn like Coolage name and bind into the Gridview when user select Row into First Gridview by using CheckBox and when click on Add Button then All Selected Row are Transfer to Another GridView and Automatically Deleted From First GridView.

in Hotel manager can easily maintain the bill of the Item that is ordered by user just select the item and move to another gridview and print the bill and generate the report.so it is very useful features for Hotel Billing Management system.

You have to simple create a one Table name is collage





So How to Move Row From one GridView to another are as Defined Below.

to show Example of How to Upload File to Database and bind to Gridview Uplaod File and Bind to gridview

How to Read and write File in Asp.Net Read and Write File


selection.aspx:-



<%@ Page Title="" Language="C#" MasterPageFile="~/Student.master" AutoEventWireup="true" CodeFile="selection.aspx.cs" Inherits="selection" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Panel ID="Panel2" runat="server">
  
<div style="height:400px; overflow:scroll; width:400px; left:10px; top:180px; position:absolute">
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Bold="True"
        Font-Size="Large" ForeColor="#333333" GridLines="None"
        AutoGenerateColumns="False">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <Columns>
   
     <asp:TemplateField HeaderText="Delete All">
       <HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server"
              AutoPostBack="true" OnCheckedChanged="c1"
              />
</HeaderTemplate>

       <ItemTemplate>
           <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="c2" />
       </ItemTemplate>
       </asp:TemplateField>

    <asp:TemplateField Visible="false">
    <ItemTemplate>
     <asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="List of Collages">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>  <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
    </div>
     </asp:Panel>
    <asp:Panel ID="Panel3" runat="server">
   
    <div style="height:400px; overflow:scroll; width:400px; left:450px; top:180px; position:absolute">
      
    <asp:GridView ID="GridView2" runat="server" CellPadding="4" Font-Bold="True"
        Font-Size="Large" ForeColor="#333333" GridLines="None"
            AutoGenerateColumns="false"  
        >
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
        <asp:TemplateField HeaderText="Select All">
       <HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server"
              AutoPostBack="true" OnCheckedChanged="c3"
              />
</HeaderTemplate>

       <ItemTemplate>
           <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="c4" />
       </ItemTemplate>
       </asp:TemplateField>

        <asp:TemplateField HeaderText="up">
        <ItemTemplate>
       
            <asp:ImageButton ID="ImageButton1" runat="server" onclick="imgbtn_Click" ImageUrl="~/up.jpeg" Width="50" Height="50" />
        </ItemTemplate>
       
        </asp:TemplateField>
        <asp:TemplateField HeaderText="down">
        <ItemTemplate>
       
            <asp:ImageButton ID="ImageButton2" runat="server" onclick="imgbtn1_Click" ImageUrl="~/down.jpeg"  Width="50" Height="50" />
        </ItemTemplate>
       
        </asp:TemplateField>
         <asp:TemplateField HeaderText="priority">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("priority") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                     <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("priority") %>'></asp:TextBox>
                 </EditItemTemplate>
                </asp:TemplateField>
           
       
         <asp:TemplateField HeaderText="Collage_name">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                     <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
                 </EditItemTemplate>
                </asp:TemplateField>
           
        </Columns>
    </asp:GridView>  <br />
        <asp:Button ID="Button3" runat="server" Text="Remove" onclick="Button3_Click" />
    </div>
    </asp:Panel>
    <asp:Panel ID="Panel1" runat="server" Visible="false">
    <center><b style="font-size: large"><marquee>Your Selection List</marquee></b><br />
        </center>
     
        <asp:Button ID="Button2" runat="server" Text="add or edit"
            onclick="Button2_Click1" />
       
    </asp:Panel>
   
    </asp:Content>


selection.aspx.cs:-



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class selection : System.Web.UI.Page
{
    DataTable ds;
    DataTable dt,dt1;
    //static DataTable dt1;
    protected void Page_Load(object sender, EventArgs e)
    {
       
    
        if (!Page.IsPostBack)
        {
            ccc = 0;
            Session["dtd1"] =null;
            Session["dtd"] = null;
        ds = new DataTable();

        string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@";

            string q = "select * from collage";
        
            SqlConnection conn = new SqlConnection(con);

            SqlCommand cmd = new SqlCommand(q, conn);
      


            SqlDataAdapter sa = new SqlDataAdapter();
            conn.Open();
           


                cmd.ExecuteNonQuery();
                sa.SelectCommand = cmd;
                sa.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                Session["dtd"] = ds;
           
       
           

        }
       
    }
  
   public static  int ccc;
    protected void Button1_Click(object sender, EventArgs e)
    {
       
        dt = Session["dtd"] as DataTable;
        dt1 = Session["dtd1"] as DataTable;
        int cnt = dt.Rows.Count;
        int cc = 0;
        if (dt1 == null)
        {
            dt1 = new DataTable();

            dt1.Columns.Add("priority").ToString();
            dt1.Columns.Add("name").ToString();
           
           
           
        }


      
      

        for(int i=0;i<cnt;i++)
        {

            CheckBox c = (CheckBox)(GridView1.Rows[i].FindControl("CheckBox1"));
          
           // string a = GridView1.Rows[grdRow.RowIndex].Cells[1].Text;
           
            if (c.Checked)
            {
                Label l = (Label)GridView1.Rows[i].FindControl("Label1");
               
               
                if (cc == 0)
                {
                    dt.Rows.RemoveAt(i);
                    DataRow dr = dt1.NewRow();


                    dr["priority"] = ccc+1;
                    dr["name"] =l.Text;
                    dt1.Rows.Add(dr);
                   
                   
                }
                else
                {
                    dt.Rows.RemoveAt(i - cc);
                    DataRow dr = dt1.NewRow();


                    dr["priority"] = ccc + 1;
                    dr["name"] = l.Text;
                    dt1.Rows.Add(dr);
                   
      
                }
                cc++;
                ccc++;
               
             
            }
           

        }
       
        GridView1.DataSource = dt;
        GridView1.DataBind();
        GridView2.DataSource = dt1;
        GridView2.DataBind();
           
        Session["dtd1"] = dt1;
      
    }
    protected void imgbtn_Click(object sender, ImageClickEventArgs e)
    {
        dt1 = Session["dtd1"] as DataTable;
        ImageButton btndetails = sender as ImageButton;
        GridViewRow row = (GridViewRow)btndetails.NamingContainer;
        if (row.RowIndex > 0)
        {
            Label l1 = (Label)row.FindControl("Label2");
            //Label l2 = (Label)row.FindControl("Label1");
            dt1.Rows[row.RowIndex][1] = dt1.Rows[row.RowIndex - 1][1].ToString();
            dt1.Rows[row.RowIndex - 1][1] = l1.Text;
            GridView2.DataSource = dt1;
            GridView2.DataBind();
            Session["dtd1"] = dt1;
        }
    }
    protected void imgbtn1_Click(object sender, ImageClickEventArgs e)
    {

        dt1 = Session["dtd1"] as DataTable;
        ImageButton btndetails = sender as ImageButton;
        GridViewRow row = (GridViewRow)btndetails.NamingContainer;
        if (row.RowIndex < GridView2.Rows.Count-1)
        {

            Label l1 = (Label)row.FindControl("Label2");
            //Label l2 = (Label)row.FindControl("Label1");
            dt1.Rows[row.RowIndex][1] = dt1.Rows[row.RowIndex + 1][1].ToString();
            dt1.Rows[row.RowIndex + 1][1] = l1.Text;
            GridView2.DataSource = dt1;
            GridView2.DataBind();
            Session["dtd1"] = dt1;
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

    }


  
    protected void Button3_Click(object sender, EventArgs e)
    {
        dt1 = Session["dtd1"] as DataTable;
        dt = Session["dtd"] as DataTable;
        int cnt = dt1.Rows.Count;
        int cc = 0;
      
        for (int i = 0; i < cnt; i++)
        {

            CheckBox c = (CheckBox)(GridView2.Rows[i].FindControl("CheckBox1"));

            // string a = GridView1.Rows[grdRow.RowIndex].Cells[1].Text;

            if (c.Checked)
            {
               // Label l = (Label)GridView1.Rows[i].FindControl("Label1");


                if (cc == 0)
                {
                    for (int j = cnt-1; j >i; j--)
                    {
                     
                       
                        dt1.Rows[j][0] = dt1.Rows[j-1][0].ToString();
                       
                    }
                    DataRow dr = dt.NewRow();
                    dr["name"] = dt1.Rows[i][1].ToString();
                    dt.Rows.Add(dr);
                   
                  
                    dt1.Rows.RemoveAt(i);
                   
                    ccc = cnt - 2;
                  

                }
                else
                {


                    for (int j =dt1.Rows.Count-1; j >=i; j--)
                    {


                        dt1.Rows[j][0] = dt1.Rows[j - 1][0].ToString();

                    }
                    DataRow dr = dt.NewRow();
                    dr["name"] = dt1.Rows[i-cc][1].ToString();
                    dt.Rows.Add(dr);
                   
                    dt1.Rows.RemoveAt(i - cc);
                   
                    ccc = dt1.Rows.Count - 1;
                }
                cc++;
                ccc++;


            }


        }

        GridView2.DataSource = dt1;
        GridView2.DataBind();
        GridView1.DataSource = dt;
        GridView1.DataBind();

        Session["dtd1"] = dt1;
        Session["dtd"] = dt;
    }
    protected void c1(object sender, EventArgs e)
    {
        dt = Session["dtd"] as DataTable;
        CheckBox chkAll =
   (CheckBox)GridView1.HeaderRow.FindControl("chkSelectAll");
        if (chkAll.Checked == true)
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                CheckBox c = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                c.Checked = true;
                GridView1.Rows[i].Style.Add("BackGround-Color", "yellow");
            }
        else
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                CheckBox c = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                c.Checked = false;
                GridView1.Rows[i].Style.Add("BackGround-Color", "white");
            }
        }



    }
  
    protected void c2(object sender, EventArgs e)
    {
        CheckBox btndetails = sender as CheckBox;
        GridViewRow row = (GridViewRow)btndetails.NamingContainer;
        CheckBox l1 = (CheckBox)row.FindControl("CheckBox1");
        if (l1.Checked)
            row.Style.Add("BackGround-Color", "yellow");
        else
            row.Style.Add("BackGround-Color", "white");
       
    }
    protected void c3(object sender, EventArgs e)
    {
        dt1 = Session["dtd1"] as DataTable;
        CheckBox chkAll =
   (CheckBox)GridView2.HeaderRow.FindControl("chkSelectAll");
        if (chkAll.Checked == true)
            for (int i = 0; i < dt1.Rows.Count; i++)
            {

                CheckBox c = (CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
                c.Checked = true;
                GridView2.Rows[i].Style.Add("BackGround-Color", "yellow");
            }
        else
        {
            for (int i = 0; i < dt1.Rows.Count; i++)
            {

                CheckBox c = (CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
                c.Checked = false;
                GridView2.Rows[i].Style.Add("BackGround-Color", "white");
            }
        }



    }
    protected void c4(object sender, EventArgs e)
    {
        CheckBox btndetails = sender as CheckBox;
        GridViewRow row = (GridViewRow)btndetails.NamingContainer;
        CheckBox l1 = (CheckBox)row.FindControl("CheckBox1");
        if (l1.Checked)
            row.Style.Add("BackGround-Color", "yellow");
        else
            row.Style.Add("BackGround-Color", "white");

    }


          
    protected void Button2_Click1(object sender, EventArgs e)
    {
        Panel3.Visible = true;
        Panel2.Visible = true;
        Panel1.Visible = false;
        ds = new DataTable();

        string con = @"Data Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@";
        string q = "select * from collage";
        SqlConnection conn = new SqlConnection(con);

             SqlCommand cmd1 = new SqlCommand(q, conn);
        SqlDataAdapter sa = new SqlDataAdapter();
        conn.Open();
         cmd1.ExecuteNonQuery();
        sa.SelectCommand = cmd1;
        sa.Fill(ds);
        dt1 = Session["dtd1"] as DataTable;
        GridView2.DataSource=dt1;
        GridView2.DataBind();
        Session["dtd"] = ds;
        dt = Session["dtd"] as DataTable;
           
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (dt.Rows[j][1].ToString().Equals(dt1.Rows[i][1].ToString()))
                    dt.Rows.RemoveAt(j);
                }
                Session["dtd"] = dt;
               
            }
          
       
       
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}


1 comments: