Friday, 24 February 2012

Gridview in Gridview or nested Gridview in Asp.Net

Create a Database name with log and then Execute the following scripts in sql server
USE [log]
GO
/****** Object:  Table [dbo].[Department]    Script Date: 02/24/2012 11:59:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Department](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [department name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF


USE [log]
GO
/****** Object:  Table [dbo].[Student_S]    Script Date: 02/24/2012 13:11:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student_S](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [Sid] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Firstname] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Phone] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [Department] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 
 CONSTRAINT [PK_Student_S] PRIMARY KEY CLUSTERED 
(
 [Sid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridauto.aspx.cs" Inherits="gridauto" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
     <h2 style=" color:blue; font-style:normal ">    
               Gridview in Gridview in asp.net    
 <br />or Nested Gridview
        </h2>
    <div>
        <asp:GridView ID="GridView1" runat="server" 
            OnRowDataBound="GridView1_RowDataBound"  HeaderStyle-ForeColor="Blue"
            AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CellPadding="4" 
            ForeColor="#333333" GridLines="None">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:TemplateField HeaderText="DEPT">
                    <ItemTemplate>
                        <!-- <asp:BoundField HeaderText="Dept" DataField="deptname" runat="server"/> -->
                        <asp:Label ID="deptname1" runat="server" Text=' <%#Eval("department name")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:GridView ID="GridView2" runat="server"  >
                        <RowStyle BackColor="#EFF3FB" />
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<HeaderStyle ForeColor="White" BackColor="#507CD1" Font-Bold="True"></HeaderStyle>
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:logConnectionString %>"
            SelectCommand="SELECT [department name] FROM [Department]"></asp:SqlDataSource>
    </div>
    
   
    </form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class gridauto : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Employee"].ToString());
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataBind();
    }
    string[] a = new string[10];
    int i = 0;
    int m = 0, n = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (GridView1.Rows.Count >= 0)
        {
            m = n;
            Label l = (Label)e.Row.FindControl("deptname1");
            a[i] = l.Text;
            if (l.Text != " ")
            {
                con.Open();
                GridView gv = (GridView)e.Row.FindControl("GridView2");
                ds.Clear();
                SqlCommand cmd = new SqlCommand("select LastName ,Firstname,Phone from Student_S where Department='" + l.Text + "'", con);
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                gv.DataSource = ds;
                gv.DataBind();
                con.Close();
            }
        }
       
    }
}
}

No comments:

Post a Comment