20 May 2013

nested gridview in asp.net c#

Create Database:

create database nestedgridview


USE [nestedgridview]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Branch](
[id] [int] IDENTITY(100,1) NOT NULL,
[Branch] [varchar](200) NULL,
[sem] [varchar](200) NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

USE [nestedgridview]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[teacher](
[id] [int] IDENTITY(100,1) NOT NULL,
[branch] [int] NULL,
[name] [varchar](200) NULL,
[cell] [varchar](200) NULL,
PRIMARY KEY CLUSTERED 
(
[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[teacher]  WITH CHECK ADD FOREIGN KEY([branch])
REFERENCES [dbo].[Branch] ([id])
GO

Create 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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <center>
            <asp:GridView ID="gv1" runat="server" AutoGenerateColumns="false" CellPadding="4"
                CellSpacing="2" OnRowDataBound="gv1_RowDataBound" DataKeyNames="id">
                <Columns>
                    <asp:TemplateField HeaderText="Nested Gridview">
                        <ItemTemplate>
                            <%# Eval("id")%>---
                            <%# Eval("Branch")%>
                            ---
                            <%# Eval("sem")%>
                            <asp:GridView ID="gv2" runat="server" Width="100%" EnableViewState="false">
                            </asp:GridView>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </center>
    </div>
    </form>
</body>
</html>

Create 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;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    SqlConnection con1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    SqlCommand cmd = new SqlCommand();
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        string sql = "select * from Branch";
        con.Open();
        cmd = new SqlCommand(sql, con);
        da.SelectCommand = cmd;
        da.Fill(ds);
        con.Close();
        gv1.DataSource = ds;
        gv1.DataBind();
    }
    protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow) return;
        GridView gv2 = (GridView)e.Row.FindControl("gv2");
        int fid = int.Parse(gv1.DataKeys[e.Row.RowIndex].Value.ToString());
       // DataTable table = new DataTable();
        DataSet ds1 = new DataSet();
        SqlConnection con1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        using (con1)
        {
           // con.Open();
            string sql1 = "select * from teacher where branch=@fid";
            using (SqlCommand cmd1 = new SqlCommand(sql1, con1))
            {
                using (SqlDataAdapter da1=new SqlDataAdapter(cmd1))
                {
                    cmd1.Parameters.AddWithValue("@fid", fid);
                    da1.Fill(ds1);
                }
            }
        }
        gv2.DataSource = ds1;
        gv2.DataBind();
       // con.Close();
       // ds1.Clear();
    }
    
    
}

web.config

  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=.;Initial Catalog=nestedgridview;Integrated Security=true;"/>
  </connectionStrings>

Output:

No comments:

Post a Comment