Example Of Nested Gridview using asp.net with C#
Default.aspx
<%@ Page Language="C#"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/control/MyMessageBox.ascx"
TagName="Message"
TagPrefix="Msg"
%>
<!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>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("[src*=plus]").live("click", function()
{
$(this).closest("tr").after("<tr><td></td><td
colspan = '999'>" + $(this).next().html()
+ "</td></tr>")
$(this).attr("src", "minus.jpeg");
});
$("[src*=minus]").live("click", function()
{
$(this).attr("src", "plus.jpeg");
$(this).closest("tr").next().remove();
});
</script>
<style type="text/css">
.div
{
background-color:#3399CC
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="div">
<Msg:Message ID="msg" runat="server" />
<table border="1" bordercolor="white">
<%--<tr>
<td>
<asp:Label
ID="Label1" ForeColor="White" runat="server"
Text="product_id"></asp:Label>
</td>
<td>
<asp:TextBox
ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:CompareValidator
ID="CompareValidator1" runat="server"
ErrorMessage="only digit is
allowed" ControlToValidate="TextBox1"
Display="Dynamic"
Font-Bold="True"
Type="Integer"
Operator="DataTypeCheck"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="Please enter id"
ControlToValidate="TextBox1"
SetFocusOnError="true"
Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>--%>
<tr>
<td>
<asp:Label ID="Label2" ForeColor="White" runat="server" Text="product_name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="rfvFirstName"
runat="server"
ControlToValidate="TextBox2"
ErrorMessage="company Name can't be left blank"
Display="Dynamic"> </asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" ForeColor="White" Text="product_description"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox3"
ErrorMessage="description name can't be left blank"
Display="Dynamic"> </asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" ForeColor="White" Text="product_quntity"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
<td>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ErrorMessage="only digit is allowed" ControlToValidate="TextBox4"
Display="Dynamic"
Font-Bold="True" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ErrorMessage="Please
enter quantity"
ControlToValidate="TextBox4" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" ForeColor="White" runat="server" Text="product_rate"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
<td>
<%--<asp:CompareValidator
ID="CompareValidator3" runat="server"
ErrorMessage="only digit is
allowed" ControlToValidate="TextBox5"
Display="Dynamic"
Font-Bold="True"
Type="Integer"
Operator="DataTypeCheck"></asp:CompareValidator>--%>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ErrorMessage="Please
enter price"
ControlToValidate="TextBox5" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button3" runat="server" CausesValidation="false" Text="update" onclick="Button3_Click" Visible="false"/>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="add" onclick="Button1_Click" />
<%--<asp:Button
ID="Button4" runat="server" Text="cleare"
onclick="Button4_Click" />--%>
</td>
</tr>
</table>
</div>
<asp:GridView ID="GridView1" DataKeyNames="pro_id"
OnRowDataBound="OnRowDataBound" runat="server" AutoGenerateColumns="False"
GridLines="None" BorderStyle="Solid" BorderWidth="1px"
BorderColor="#df5015"
onrowediting="GridView1_RowEditing"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowupdating="GridView1_RowUpdating"
onrowdeleting="GridView1_RowDeleting" >
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<RowStyle BackColor="#E1E1E1" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor:pointer" height="20px" width="20px" src="plus.jpeg" />
<asp:Panel ID="Panel1" style="display:none" runat="server">
<asp:GridView ID="GridView2" runat="server"
OnRowDataBound="OnRowDataBound1"
AutoGenerateColumns="False"
BorderStyle="Double" DataKeyNames="pro_id"
BorderColor="#df5015"
GridLines="None"
Width="250px">
<HeaderStyle BackColor="#df5015"
Font-Bold="true"
ForeColor="White"
/>
<RowStyle BackColor="#E1E1E1"
/>
<AlternatingRowStyle BackColor="White"
/>
<HeaderStyle BackColor="#df5015"
Font-Bold="true"
ForeColor="White"
/>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor:pointer"
height="20px"
width="20px"
src="plus.jpeg"
/>
<asp:Panel ID="Panel2" style="display:none"
runat="server">
<asp:GridView ID="GridView3"
runat="server"
onrowediting="GridView3_RowEditing"
AutoGenerateColumns="false"
BorderStyle="Double" BorderColor="#df5015" GridLines="None" Width="250px">
<HeaderStyle BackColor="#df5015"
Font-Bold="true"
ForeColor="White"
/>
<RowStyle BackColor="#E1E1E1"
/>
<AlternatingRowStyle
BackColor="White"
/>
<HeaderStyle BackColor="#df5015"
Font-Bold="true"
ForeColor="White"
/>
<Columns>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="Label6" runat="server"
Text='<%#Eval("total")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Grand_Total">
<ItemTemplate>
<asp:Label ID="Label7" runat="server"
Text='<%#Eval("gtotal")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton3"
CommandName="Edit"
runat="server">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:BoundField DataField="total"
HeaderText="total" />
<asp:BoundField DataField="gtotal"
HeaderText="gtotal" />--%>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%#Eval("pro_id")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pro_name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%#Eval("pro_name")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField >
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%#Eval("quantity")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rate">
<ItemTemplate>
<asp:Label ID="Label3" runat="server"
Text='<%#Eval("rate")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton4"
runat="server"
CausesValidation="false"
onclick="LinkButton4_Click">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton5"
CausesValidation="false"
OnClick="delete"
runat="server">delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:BoundField DataField="pro_name"
HeaderText="pro_name" />
<asp:BoundField DataField="quantity"
HeaderText="quantity" />
<asp:BoundField DataField="rate"
HeaderText="rate" />--%>
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pro_Name">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("pro_name")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1"
Text='<%#Eval("pro_name")
%>' runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pro_description">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%#Eval("pro_desc")
%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" Text='<%#Eval("pro_desc")
%>' runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" CausesValidation="false" CommandName="Edit" runat="server">Edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="up" runat="server" CausesValidation="false" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="cn" runat="server" CausesValidation="false" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CausesValidation="false" CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:BoundField
DataField="pro_name" HeaderText="pro_name" />
<asp:BoundField
DataField="pro_desc" HeaderText="pro_desc" />--%>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</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
{
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
grid();
}
}
public void grid()
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
q = "select * from pi_productex ";
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();
conn.Close();
}
protected void Button1_Click(object
sender, EventArgs e)
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
q = "insert into
pi_productex(pro_name,pro_desc) values ('" + TextBox2.Text + "','" + TextBox3.Text + "')";
conn.Open();
SqlCommand
cmd = new SqlCommand(q,
conn);
int i =
cmd.ExecuteNonQuery();
if (i
> 0)
{
SqlDataAdapter
da = new SqlDataAdapter("select max(pro_id) as pro_id from
pi_productex", conn);
DataTable
dt11 = new DataTable();
da.Fill(dt11);
double
total = double.Parse(TextBox4.Text) * double.Parse(TextBox5.Text);
string q1 = "insert
into pi_rate(pro_id,quantity,rate,total) values ('" + dt11.Rows[0]["pro_id"].ToString() + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + total + "')";
SqlCommand
cmd1 = new SqlCommand(q1,
conn);
i = cmd1.ExecuteNonQuery();
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
}
grid();
}
/*protected void
GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType ==
DataControlRowType.DataRow)
{
string pro_id =
Convert.ToString(DataBinder.Eval(e.Row.DataItem, "pro_id"));
GridView gvChildGrid =
(GridView)e.Row.FindControl("GridView2");
string con = @"Data
Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection conn = new
SqlConnection(con);
string q = "select * from pi_rate
where pro_id ='" + pro_id + "'";
SqlDataAdapter da = new
SqlDataAdapter();
DataTable ds = new DataTable();
SqlCommand cmd1 = new SqlCommand(q,
conn);
conn.Open();
da.SelectCommand = cmd1;
cmd1.ExecuteNonQuery();
da.Fill(ds);
gvChildGrid.DataSource = ds;
gvChildGrid.DataBind();
conn.Close();
}
}*/
protected void OnRowDataBound(object
sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType == DataControlRowType.DataRow)
{
Session["pro_id"]
= GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
GridView
grd2 = e.Row.FindControl("GridView2")
as GridView;
Label
l =(Label)grd2.FindControl("Label9");
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
q1 = "select * from pi_rate where
pro_id='" + Session["pro_id"]
+ "'";
SqlDataAdapter
sda = new SqlDataAdapter(q1,
conn);
DataTable
sds = new DataTable();
sda.Fill(sds);
if
(sds.Rows.Count > 0)
{
//string
q = "select p.pro_name,pj.quantity,pj.rate,pj.total,p.gtotal from
pi_productex p inner join pi_rate pj on p.pro_id=pj.pro_id";
string
q = "select pj.pro_id,
p.pro_name,pj.quantity,pj.rate from pi_productex p inner join pi_rate pj on p.pro_id = pj.pro_id where
p.pro_id='" + Session["pro_id"].ToString()
+ "'";
SqlDataAdapter
da = new SqlDataAdapter(q,
conn);
DataTable
ds = new DataTable();
//
SqlCommand cmd1 = new SqlCommand(q, conn);
conn.Open();
//da.SelectCommand
= cmd1;
//cmd1.ExecuteNonQuery();
da.Fill(ds);
grd2.DataSource = ds;
//GetData(string.Format("select
p.pro_name,pj.quantity,pj.rate,pj.total,p.gtotal from pi_productex p inner join
pi_rate pj on p.pro_id=pj.pro_id", customerId));
grd2.DataBind();
}
else
{
//ClientScript.RegisterClientScriptBlock(this.GetType(),
"key", "<script> alert('It hava not child
record');</script>");
//grd2.DataSource
= sds.Rows[]["Record not Found"];
//grd2.DataBind();
//msg.Show(MyMessageBox.MessageType.Warning,
"Record Not Found");
//l.Text =
"Record is not found";
}
}
}
protected void OnRowDataBound1(object
sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType == DataControlRowType.DataRow)
{
//string
pro_id = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
GridView
grd3 = e.Row.FindControl("GridView3")
as GridView;
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
//string
q = "select p.pro_name,pj.quantity,pj.rate,pj.total,p.gtotal from
pi_productex p inner join pi_rate pj on p.pro_id=pj.pro_id";
string
q = "select pj.total,p.gtotal from
pi_productex p inner join pi_rate pj on
p.pro_id = pj.pro_id where p.pro_id='" +Session["pro_id"].ToString() + "'";
SqlDataAdapter
da = new SqlDataAdapter(q,
conn);
DataTable
ds = new DataTable();
//
SqlCommand cmd1 = new SqlCommand(q, conn);
conn.Open();
//da.SelectCommand
= cmd1;
//cmd1.ExecuteNonQuery();
da.Fill(ds);
grd3.DataSource = ds;
//GetData(string.Format("select
p.pro_name,pj.quantity,pj.rate,pj.total,p.gtotal from pi_productex p inner join
pi_rate pj on p.pro_id=pj.pro_id", customerId));
grd3.DataBind();
}
}
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
grid();
//TextBox tx
= (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("TextBox1");
}
protected void GridView1_RowUpdating(object
sender, GridViewUpdateEventArgs e)
{
Session["pro_id"]
= GridView1.DataKeys[e.RowIndex].Value.ToString();
TextBox
pnm = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
TextBox
pdesc = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
conn.Open();
string
q = "update pi_productex set pro_name='"
+ pnm.Text + "',pro_desc='" +
pdesc.Text + "' where pro_id='" +
Session["pro_id"].ToString() + "'";
SqlCommand
cmd = new SqlCommand(q,
conn);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
grid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs
e)
{
GridView1.EditIndex = -1;
grid();
}
protected void GridView3_RowEditing(object
sender, GridViewEditEventArgs e)
{
}
protected void LinkButton4_Click(object
sender, EventArgs e)
{
LinkButton
l1 = (LinkButton)sender;
GridViewRow
rw = (GridViewRow)l1.NamingContainer;
Label
pid = (Label)rw.FindControl("Label8");
Session["data"]
= pid.Text;
Label
qun = (Label)rw.FindControl("Label2");
TextBox4.Text = qun.Text;
Label
rate = (Label)rw.FindControl("Label3");
TextBox5.Text = rate.Text;
TextBox2.Visible = false;
TextBox3.Visible = false;
Button3.Visible = true;
Button1.Visible = false;
}
protected void delete(object
sender, EventArgs e)
{
LinkButton
l2 = (LinkButton)sender;
GridViewRow
rw = (GridViewRow)l2.NamingContainer;
Label
id = (Label)rw.FindControl("Label8");
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
conn.Open();
//double
total = double.Parse(TextBox4.Text) * double.Parse(TextBox5.Text);
string
q = "delete from pi_rate where pro_id='"+id.Text+"'";
SqlCommand
cmd = new SqlCommand(q,
conn);
int
i=cmd.ExecuteNonQuery();
if
(i > 0)
{
msg.Show(MyMessageBox.MessageType.Success,
"Delete Child Record successfull");
}
else
{
msg.Show(MyMessageBox.MessageType.Success,
"SORRY TRY AGAIN !!!!");
}
grid();
}
protected void Button3_Click(object
sender, EventArgs e)
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
conn.Open();
double
total = double.Parse(TextBox4.Text) * double.Parse(TextBox5.Text);
string
q = "update pi_rate set quantity='"
+ TextBox4.Text + "',rate='" +
TextBox5.Text + "',total='" +
total + "' where pro_id='" +
Session["data"].ToString() + "'";
SqlCommand
cmd = new SqlCommand(q,
conn);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
grid();
Button1.Visible = true;
Button3.Visible = false;
TextBox2.Visible = true;
TextBox3.Visible = true;
TextBox4.Text = "";
TextBox5.Text = "";
}
protected void GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
try
{
string
pro_id = GridView1.DataKeys[e.RowIndex].Value.ToString();
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
conn.Open();
string
q = "select * from pi_rate where
pro_id='" + pro_id + "'";
//string
q = "delete pi_productex where pro_id='" + pro_id + "'";
//SqlCommand
cmd = new SqlCommand(q, conn);
SqlDataAdapter
dr = new SqlDataAdapter(q,
conn);
//int i =
cmd.ExecuteNonQuery()
DataTable
dt = new DataTable();
dr.Fill(dt);
if
(dt.Rows.Count>0)
{
//ClientScript.RegisterClientScriptBlock(this.GetType(),
"key", "<script> alert('It hava child
record');</script>");
msg.Show(MyMessageBox.MessageType.Warning,"It Have Child Record");
}
else
{
string
q1 = "delete pi_productex where pro_id='"
+ pro_id + "'";
SqlCommand
cmd1 = new SqlCommand(q1,
conn);
int
i=cmd1.ExecuteNonQuery();
if
(i > 0)
{
msg.Show(MyMessageBox.MessageType.Warning,
"Delete Parant Record Successfull");
}
else
{
msg.Show(MyMessageBox.MessageType.Success,
"SORRY TRY AGAIN !!!!");
}
grid();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
0 comments :