Insert Record in gridview footer and display gridview without Record using asp.net with C#
Default.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 runat="server">
<title></title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="Gridview1" runat="server" DataKeyNames="id"
AutoGenerateColumns="false" CssClass="Gridview"
HeaderStyle-BackColor="#61A6F8"
ShowFooter="true" HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="White"
Height="200px" Width="517px"
onrowdatabound="Gridview1_RowDataBound"
onrowcommand="Gridview1_RowCommand" onrowdeleting="Gridview1_RowDeleting"
onrowediting="Gridview1_RowEditing" >
<Columns>
<asp:TemplateField HeaderText="EDIT/DELETE">
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/edit-icon.png" ToolTip="Edit" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/file_delete.png" ToolTip="Delete"
Height="20px"
Width="20px"
/>
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server"
ImageUrl="~/add.png"
CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User"
ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="F_Name">
<ItemTemplate>
<asp:Label ID="lblitemUsr"
runat="server"
Text='<%#Eval("f_name")
%>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last_name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("l_nm")
%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<asp:Label ID="lblstate"
runat="server"
Text='<%#Eval("statename")%>'/>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddl_state" AutoPostBack="true" onselectedindexchanged="ddl_state_SelectedIndexChanged" DataTextField="statename"
DataValueField="id"
runat="server"
Width="100px">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblcity"
runat="server" Text='<%#Eval("city")
%>'/>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddl_city" DataTextField="city" AutoPostBack="true"
runat="server"
Width="100px">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Default.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
{
string con
= @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
grid();
}
}
public void grid()
{
SqlConnection
conn = new SqlConnection(con);
conn.Open();
SqlDataAdapter
dap = new SqlDataAdapter("select * from pi_demo", con);
DataTable
dt = new DataTable();
dap.Fill(dt);
conn.Close();
if
(dt.Rows.Count > 0)
{
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
else
{
DataTable
dt1 = new DataTable();
//dt.Columns.Add("name");
//dt.Columns.Add("state");
//dt.Columns.Add("city");
dt.Rows.Add(new object[] { null, null });
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
}
protected void ddl_state_SelectedIndexChanged(object sender, EventArgs
e)
{
citybind();
}
public void citybind()
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
DataTable
dt = new DataTable();
conn.Open();
DropDownList
ddl4 = this.Gridview1.FooterRow.FindControl("ddl_state") as
DropDownList;
DropDownList
ddl5 = this.Gridview1.FooterRow.FindControl("ddl_city") as
DropDownList;
int x =
(ddl4.SelectedIndex);
string
q = "select city from pi_citydemo where id
='" + x + "'";
DataSet
ds = new DataSet();
SqlDataAdapter
da = new SqlDataAdapter(q,
conn);
da.Fill(ds);
ddl5.DataSource = ds;
ddl5.DataBind();
conn.Close();
}
protected void Gridview1_RowDataBound(object
sender, GridViewRowEventArgs e)
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
conn.Close();
if
(e.Row.RowType == DataControlRowType.Footer)
{
Control
ctrl1 = e.Row.FindControl("ddl_state");
if
(ctrl1 != null)
{
DropDownList
dd1 = ctrl1 as DropDownList;
dd1.Items.Insert(0, new ListItem("--select--"));
conn.Open();
SqlCommand
cmd1 = new SqlCommand("select statename from pi_statedemo",
conn);
SqlDataReader
dr1;
dr1 = cmd1.ExecuteReader();
while
(dr1.Read())
{
//D2.Items.Add(dr[0].ToString());
dd1.Items.Add(dr1[0].ToString());
}
dr1.Close();
}
conn.Close();
}
}
protected void Gridview1_RowCommand(object
sender, GridViewCommandEventArgs e)
{
if
(e.CommandName.Equals("AddNew"))
{
TextBox
tx1 = (TextBox)Gridview1.FooterRow.FindControl("TextBox1");
TextBox
tx2 = (TextBox)Gridview1.FooterRow.FindControl("TextBox2");
DropDownList
ddl4 = this.Gridview1.FooterRow.FindControl("ddl_state") as
DropDownList;
DropDownList
ddl5 = this.Gridview1.FooterRow.FindControl("ddl_city") as
DropDownList;
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
conn.Open();
string
q = "insert into
pi_demo(f_name,l_nm,statename,city)values('" + tx1.Text + "','" + tx2.Text + "','" + ddl4.SelectedValue.ToString() + "','" + ddl5.SelectedValue.ToString() + "')";
SqlCommand
cmd = new SqlCommand(q,
conn);
int i=
cmd.ExecuteNonQuery();
if
(i > 0)
{
Response.Write("<script> alert('your data is
insert')</script>");
}
else
{
Response.Write("<script> alert('Sorry Baka tara data add
nathi thaya ')</script>");
}
grid();
}
}
protected void Gridview1_RowEditing(object
sender, GridViewEditEventArgs e)
{
Label
f_nm = (Label)Gridview1.Rows[e.NewEditIndex].FindControl("lblitemUsr");
Label
l_nm = (Label)Gridview1.Rows[e.NewEditIndex].FindControl("Label1");
Label
stnm = (Label)Gridview1.Rows[e.NewEditIndex].FindControl("lblstate");
Label
cnm = (Label)Gridview1.Rows[e.NewEditIndex].FindControl("lblcity");
TextBox
tx1 = (TextBox)Gridview1.FooterRow.FindControl("TextBox1");
TextBox
tx2 = (TextBox)Gridview1.FooterRow.FindControl("TextBox2");
DropDownList
ddls = (DropDownList)Gridview1.FooterRow.FindControl("ddl_state");
DropDownList
ddlc = (DropDownList)Gridview1.FooterRow.FindControl("ddl_city");
tx1.Text = f_nm.Text;
tx2.Text = l_nm.Text;
ddls.SelectedValue = stnm.Text;
citybind();
//ddlc.SelectedValue
= cnm.Text;
}
protected void Gridview1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
id = Gridview1.DataKeys[e.RowIndex].Value.ToString();
conn.Open();
string
q = "delete from pi_demo where id='"+id.ToString()+"'";
SqlCommand
cmd = new SqlCommand(q,
conn);
int i =
cmd.ExecuteNonQuery();
if (i
> 0)
{
Response.Write("<script> alert('your data is
delete')</script>");
}
else
{
Response.Write("<script> alert('Sorry Baka tara data delete
nathi thaya ')</script>");
}
grid();
}
}
0 comments :