temporarygridviews using asp.net with C#
design.aspx
<%@ Page Language="C#"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1"
runat="server">
<style type ="text/css">
</style>
<title></title>
</head>
<body bgcolor="#3399CC">
<form id="form1" runat="server">
<div>
<center><table border="1"
bordercolor="black">
<tr>
<td>
<asp:Label ID="Label6" ForeColor="White" runat="server" Text="company id"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
<td>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="only digit is allowed" ControlToValidate="TextBox6"
Display="Dynamic"
Font-Bold="True" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="Please
enter id"
ControlToValidate="TextBox6" SetFocusOnError="true"
Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" ForeColor="White" runat="server"
Text="company name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="rfvFirstName"
runat="server"
ControlToValidate="TextBox1"
ErrorMessage="company Name can't be left blank"
Display="Dynamic"> </asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" ForeColor="White" runat="server"
Text="persion
name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2"
ErrorMessage="person name can't be left blank"
Display="Dynamic"> </asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td >
<asp:Label ID="Label3" ForeColor="White" runat="server"
Text="company phone"></asp:Label>
</td>
<td >
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator4"
runat="server"
ControlToValidate="TextBox3"
ErrorMessage="phoneno can't be left blank"
SetFocusOnError="True"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3"
runat="server"
ErrorMessage="only number is allowed and 6 digit number"
ControlToValidate="TextBox3"
Display="Dynamic"
ValidationExpression="^[0-9]{6}"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" ForeColor="White" runat="server"
Text="company
mobile"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5"
runat="server"
ControlToValidate="TextBox4"
ErrorMessage="cellno can't be left blank"
SetFocusOnError="True"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
runat="server"
ErrorMessage="only number is allowed"
ControlToValidate="TextBox4"
Display="Dynamic"
ValidationExpression="^[0-9]{10}"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" ForeColor="White" runat="server"
Text="company
address"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox5"
ErrorMessage="Address can't be left blank"
Display="Dynamic"> </asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button4" runat="server" Text="update"
onclick="Button4_Click"
Visible="false" CausesValidation="False"/></td>
<td>
<asp:Button ID="Button1"
runat="server"
Text="add"
onclick="Button1_Click" />
<asp:Button ID="Button3" runat="server" Text="clear"
onclick="Button3_Click"
CausesValidation="False" />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="com_id">
<Columns>
<asp:TemplateField HeaderText="checked">
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2"
runat="server"
AutoPostBack="true"
oncheckedchanged="CheckBox2_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1"
runat="server"
AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%#Eval("com_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="c_nm">
<ItemTemplate>
<asp:Label ID="Label7" runat="server"
Text='<%#Eval("com_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox7"
runat="server"
Text='<%#Eval("com_name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="p_nm">
<ItemTemplate>
<asp:Label ID="Label9" runat="server"
Text='<%#Eval("person_nm") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox9"
runat="server"
Text='<%#Eval("person_nm") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ph_no">
<ItemTemplate>
<asp:Label ID="Label10"
runat="server"
Text='<%#Eval("ph_no") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox10"
runat="server"
Text='<%#Eval("ph_no") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="mo_no">
<ItemTemplate>
<asp:Label ID="Label11"
runat="server"
Text='<%#Eval("mo_no") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox11"
runat="server"
Text='<%#Eval("mo_no") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="c_add">
<ItemTemplate>
<asp:Label ID="Label12"
runat="server"
Text='<%#Eval("com_add") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox12"
runat="server"
Text='<%#Eval("com_add") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="edit">
<ItemTemplate>
<%--<asp:Button ID="Button3"
runat="server"
Text="edit"
/>--%>
<asp:LinkButton ID="LinkButton1"
runat="server" OnClick="edit" CommandArgument='<%#Eval("com_id")
%>'
CausesValidation="False">edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2"
CommandName="update" runat="server">update</asp:LinkButton>
<asp:LinkButton ID="LinkButton3"
runat="server" CommandName="cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton4"
runat="server" OnClick="delete"
CommandArgument='<%#Eval("com_id")
%>'
CausesValidation="False">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button2" runat="server" Text="submit"
onclick="Button2_Click" />
<asp:Button ID="Button5" runat="server" Text="delete all"
onclick="Button5_Click" />
</center>
</div>
</form>
</body>
</html>
design.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;
using
System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
DataTable
dt;
DataTable
dt1;
protected
void Page_Load(object
sender, EventArgs e)
{
if
(Page.IsPostBack)
{
dt = Session["data_table"] as
DataTable;
}
if
(!Page.IsPostBack)
{
Session["data_table"] = null;
}
if
(Request.QueryString["c_id"] != null)
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=DemoD;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
q = "select * from pi_comp where
com_id='" + Request.QueryString["c_id"].ToString()
+ "'";
SqlDataAdapter
da = new SqlDataAdapter();
DataTable
ds = new DataTable();
SqlCommand
cmd1 = new SqlCommand(q,
conn);
conn.Open();
da.SelectCommand = cmd1;
cmd1.ExecuteNonQuery();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
dt = ds;
conn.Close();
}
}
protected void Button1_Click(object
sender, EventArgs e)
{
if (dt
== null)
{
dt = new
DataTable();
DataColumn
dc0 = new DataColumn("com_id");
DataColumn
dc1 = new DataColumn("com_name");
DataColumn
dc2 = new DataColumn("person_nm");
DataColumn
dc3 = new DataColumn("ph_no");
DataColumn
dc4 = new DataColumn("mo_no");
DataColumn
dc5 = new DataColumn("com_add");
dt.Columns.Add(dc0);
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
}
DataRow
dr1 = dt.NewRow();
dr1[0] = TextBox6.Text;
dr1[1] = TextBox1.Text;
dr1[2] = TextBox2.Text;
dr1[3] = TextBox3.Text;
dr1[4] = TextBox4.Text;
dr1[5] = TextBox5.Text;
dt.Rows.Add(dr1);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["data_table"]
= dt;
}
protected void Button2_Click(object
sender, EventArgs e)
{
string
q;
int i =
0;
for (int r = 0; r < dt.Rows.Count; r++)
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=DemoD;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
q = "insert
into pi_comp(com_id,com_name,person_nm,ph_no,mo_no,com_add) values ("
+ dt.Rows[r][0].ToString() + ",'"
+ dt.Rows[r][1].ToString() + "','"
+ dt.Rows[r][2].ToString() + "','"
+ dt.Rows[r][3].ToString() + "','"
+ dt.Rows[r][4].ToString() + "','"
+ dt.Rows[r][5].ToString() + "')";
//+ g1.Cells[5].Text +
SqlCommand
cmd = new SqlCommand(q,
conn);
conn.Open();
i = cmd.ExecuteNonQuery();
conn.Close();
}
if (i
> 0)
{
Response.Redirect("~/showdata.aspx");
}
}
protected void edit(object
sender, EventArgs e)
{
Session["id"]
= ((LinkButton)sender).CommandArgument;
foreach
(GridViewRow r in
GridView1.Rows)
{
string
sid = GridView1.DataKeys[r.RowIndex].Values[0].ToString();
if
(sid.Equals(Session["id"].ToString()))
{
Label
cid = (Label)GridView1.Rows[r.RowIndex].FindControl("Label8");
TextBox6.Text = cid.Text;
Label
cnm = (Label)GridView1.Rows[r.RowIndex].FindControl("Label7");
TextBox1.Text = cnm.Text;
Label
pnm = (Label)GridView1.Rows[r.RowIndex].FindControl("Label9");
TextBox2.Text = pnm.Text;
Label
pno = (Label)GridView1.Rows[r.RowIndex].FindControl("Label10");
TextBox3.Text = pno.Text;
Label
mno = (Label)GridView1.Rows[r.RowIndex].FindControl("Label11");
TextBox4.Text = mno.Text;
Label
cadd = (Label)GridView1.Rows[r.RowIndex].FindControl("Label12");
TextBox5.Text = cadd.Text;
TextBox6.ReadOnly = true;
}
}
Button1.Visible = false;
Button4.Visible = true;
}
protected void Button3_Click(object
sender, EventArgs e)
{
TextBox6.Text = "";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
Button1.Visible = true;
Button4.Visible = false;
}
protected void Button4_Click(object
sender, EventArgs e)
{
if
(Request.QueryString["c_id"] == null)
{
string
sid = null;
//string t
= TextBox6.Text;
foreach
(GridViewRow r in
GridView1.Rows)
{
sid =
GridView1.DataKeys[r.RowIndex].Values[0].ToString();
if
(sid.Equals(Session["id"].ToString()))
{
dt.Rows[r.RowIndex][0] =
TextBox6.Text;
dt.Rows[r.RowIndex][1] =
TextBox1.Text;
dt.Rows[r.RowIndex][2] =
TextBox2.Text;
dt.Rows[r.RowIndex][3] =
TextBox3.Text;
dt.Rows[r.RowIndex][4] = TextBox4.Text;
dt.Rows[r.RowIndex][5] =
TextBox5.Text;
TextBox6.ReadOnly = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Button4.Visible = false;
Button1.Visible = true;
}
else
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=DemoD;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
q = "update pi_comp set com_name='"
+ TextBox1.Text + "',person_nm='"
+ TextBox2.Text + "',ph_no='" +
TextBox3.Text + "',mo_no='" +
TextBox4.Text + "',com_add='" +
TextBox5.Text + "' where com_id='"
+ Request.QueryString["c_id"].ToString()
+ "'";
SqlCommand
cmd = new SqlCommand(q,
conn);
DataSet
ds = new DataSet();
SqlDataAdapter
da = new SqlDataAdapter();
conn.Open();
//da.SelectCommand
= cmd;
int
i = cmd.ExecuteNonQuery();
if
(i > 0)
{
Response.Redirect("~/showdata.aspx");
}
//
da.Fill(ds);
//
GridView1.DataSource = ds;
//GridView1.DataBind();
conn.Close();
}
}
protected void delete(object
sender, EventArgs e)
{
Session["id1"]
= ((LinkButton)sender).CommandArgument;
foreach
(GridViewRow r in
GridView1.Rows)
{
string
sid = GridView1.DataKeys[r.RowIndex].Values[0].ToString();
if
(sid.Equals(Session["id1"]))
{
dt.Rows.RemoveAt(r.RowIndex);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
CheckBox
chk = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox2");
if
(chk.Checked == true)
{
foreach
(GridViewRow r in
GridView1.Rows)
{
CheckBox
chk1 = (CheckBox)GridView1.Rows[r.RowIndex].FindControl("CheckBox1");
chk1.Checked = true;
}
}
else
{
foreach
(GridViewRow r in
GridView1.Rows)
{
CheckBox
chk1 = (CheckBox)GridView1.Rows[r.RowIndex].FindControl("CheckBox1");
chk1.Checked = false;
}
}
}
protected void Button5_Click(object
sender, EventArgs e)
{
int j =
0;
for (int i = 0; i
< GridView1.Rows.Count; i++)
{
CheckBox
chk1 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if
(chk1 != null)
{
if
(chk1.Checked)
{
if (i
== 0)
{
dt.Rows.RemoveAt(i);
}
else
{
j++;
dt.Rows.RemoveAt(i -
j);
}
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
views.aspx
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="showdata.aspx.cs"
Inherits="showdata"
%>
<!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 id="Head1"
runat="server">
<title></title>
</head>
<body bgcolor="#3399CC">
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView2" BorderStyle="Solid"
runat="server"
ForeColor="White" BorderColor="Black" AutoGenerateColumns="False"
onrowediting="GridView2_RowEditing" onrowdeleting="GridView2_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%#Eval("com_id")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="c_nm">
<ItemTemplate>
<asp:Label ID="Label7" runat="server"
Text='<%#Eval("com_name")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox7"
runat="server"
Text='<%#Eval("com_name")
%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="p_nm">
<ItemTemplate>
<asp:Label ID="Label9" runat="server"
Text='<%#Eval("person_nm")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox9"
runat="server"
Text='<%#Eval("person_nm")
%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ph_no">
<ItemTemplate>
<asp:Label ID="Label10"
runat="server"
Text='<%#Eval("ph_no")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox10"
runat="server"
Text='<%#Eval("ph_no")
%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="mo_no">
<ItemTemplate>
<asp:Label ID="Label11"
runat="server"
Text='<%#Eval("mo_no")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox11"
runat="server"
Text='<%#Eval("mo_no")
%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="c_add">
<ItemTemplate>
<asp:Label ID="Label12"
runat="server"
Text='<%#Eval("com_add")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox12"
runat="server"
Text='<%#Eval("com_add")
%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="edit">
<ItemTemplate>
<%--<asp:Button ID="Button3"
runat="server" Text="edit" />--%>
<asp:LinkButton ID="LinkButton1"
runat="server"
CommandName="edit" CommandArgument='<%#Eval("com_id")
%>' >edit</asp:LinkButton>
</ItemTemplate>
<%--<EditItemTemplate>
<asp:LinkButton ID="LinkButton2"
CommandName="update"
runat="server">update</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server"
CommandName="cancel">Cancel</asp:LinkButton>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton4"
runat="server"
CommandName="Delete"
>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
views.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;
using
System.Data.SqlClient;
public partial class showdata : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
grid();
}
public void grid()
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=DemoD;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
q = "select * from pi_comp";
DataSet
ds = new DataSet();
SqlDataAdapter
da = new SqlDataAdapter();
SqlCommand
cmd1 = new SqlCommand(q,
conn);
conn.Open();
da.SelectCommand = cmd1;
cmd1.ExecuteNonQuery();
da.Fill(ds);
GridView2.DataSource = ds;
GridView2.DataBind();
conn.Close();
}
protected void GridView2_RowEditing(object
sender, GridViewEditEventArgs e)
{
Label
cid = (Label)GridView2.Rows[e.NewEditIndex].FindControl("Label8");
Label cnm = (Label)GridView2.Rows[e.NewEditIndex].FindControl("Label7");
Label
pnm = (Label)GridView2.Rows[e.NewEditIndex].FindControl("Label9");
Label
pno = (Label)GridView2.Rows[e.NewEditIndex].FindControl("Label10");
Label
mno = (Label)GridView2.Rows[e.NewEditIndex].FindControl("Label11");
Label
cadd = (Label)GridView2.Rows[e.NewEditIndex].FindControl("Label12");
Response.Redirect("~/Default.aspx?c_id=" + cid.Text + "");
}
protected void GridView2_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
Label
cid = (Label)GridView2.Rows[e.RowIndex].FindControl("Label8");
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=DemoD;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
q = "delete from pi_comp where com_id='"
+ cid.Text + "'";
SqlCommand
cmd1 = new SqlCommand(q,
conn);
conn.Open();
cmd1.ExecuteNonQuery();
GridView2.EditIndex = -1;
grid();
conn.Close();
}
}
0 comments :