Description:-
In this Example We Explain that how to delete
more than one row in temporary grid view or DataTable selected by check-box in
asp.net.
Here we
create a Dynamically DataTable and add
Rows to datatable and bind it to the gridview with edit,update and delete
facility in gridview.the main advantage of this example is that we can insert
update delete record in Datatable not on sqlserver.this is very useful
procedure for temporary item maintain like in online shopping or in hotel to
manage the list menu of the customer.
In which we Create two Gridview
First Gridview Contain All Collages List in which You can select Multiple
Collage based on CheckBox and Selected Record are Bind to the Second GridView
and in First Gridview selected Record are Deleted Automatically.
In Second Gridview You can Remove Multiple Record By
selecting Record through CheckBox. Also you can Up and Down the Record means
Replace Record with Each Record by using up and Down arrow key Image.when you
select Record throuch checkbox then selected Rows color are Automatically
Change.
we uses the dataTable and DataRow to store Record
and Bind Record and for Delete the Record because of Temporary operation not
perfomed in the dataBase.
the Main point is that delete more than one row in temporary grid view.it means that there is no affection ti the database only operation is performed in Front End Not in Back End.
To Show Example of Display Selection Of Multiple Row based on Checkbox Selection in MVC Click Here Select Multiple row through CheckBox in MVC
Aspx Code:-
<%@ 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" />
<asp:Button ID="Button4" runat="server" Text="save" onclick="Button4_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 />
<asp:GridView ID="GridView3" runat="server">
<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" />
</asp:GridView>
</center>
<asp:Button ID="Button2" runat="server" Text="add or edit"
onclick="Button2_Click1" />
</asp:Panel>
</asp:Content>
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";
string q1 = "select
priority,name from oe_final where mid=1";
SqlConnection conn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(q, conn);
SqlCommand cmd1 = new SqlCommand(q1, conn);
SqlDataAdapter sa = new SqlDataAdapter();
conn.Open();
cmd1.ExecuteNonQuery();
sa.SelectCommand = cmd1;
sa.Fill(ds);
if (ds.Rows.Count > 0)
{
Panel1.Visible = true;
GridView3.DataSource = ds;
GridView3.DataBind();
Panel2.Visible = false;
Panel3.Visible = false;
Session["dtd1"] = ds;
ccc = ds.Rows.Count;
}
else
{
cmd.ExecuteNonQuery();
sa.SelectCommand = cmd;
sa.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Session["dtd"] = ds;
//ccc
= ds.Rows.Count;
}
}
}
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 Button4_Click(object sender, EventArgs e)
{
string con = @"Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@";
string del = "delete from
oe_final where mid='" + Session["mid"].ToString()
+ "'";
SqlConnection cn = new SqlConnection(con);
SqlCommand cmd = new SqlCommand(del, cn);
cn.Open();
cmd.ExecuteNonQuery();
dt1 = Session["dtd1"] as DataTable;
int y=0;
string sel= Session["mid"].ToString();
for (int i = 0; i < dt1.Rows.Count; i++)
{
String q2 = "insert into
oe_final (mid,priority,name)values('" + sel +
"','" + dt1.Rows[i][0].ToString() + "','" +
dt1.Rows[i][1].ToString() + "')";
SqlCommand cmd1 = new SqlCommand(q2, cn);
y = cmd1.ExecuteNonQuery();
}
if(y>0)
ClientScript.RegisterClientScriptBlock(this.GetType(), "key", "<script> alert('submited
successfully');</script>");
Panel2.Visible = false;
Panel3.Visible = false;
Panel1.Visible = true;
grid();
//
Session["dtd1"] = (DataTable)dt1;
}
//public
DataTable f1()
//{
// string con = @"Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@";
// string nm = "select priority,name
from oe_final where mid='" + sr[0].ToString() + "'";
// SqlCommand cmd3 = new SqlCommand(nm, cn);
// SqlDataReader sr1 = cmd3.ExecuteReader();
// while (sr1.Read())
// {
//}
public void grid()
{
dt1 = Session["dtd1"] as DataTable;
GridView3.DataSource = dt1;
GridView3.DataBind();
}
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 q1 = "select priority,name from oe_final where mid='" +
Session["mid"].ToString() + "'";
string q = "select * from
collage";
SqlConnection conn = new SqlConnection(con);
//SqlCommand
cmd = new SqlCommand(q1, conn);
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();
//ccc =
dt1.Rows.Count;
}
}
0 comments:
Post a Comment