Friday, 25 October 2013

How to Bind Nested Gridview with expand collapse from Database in asp.net with C#/VB.NET

Hi friends,in this article I will explain about How to Bind Nested Gridview from Database in asp.net with C#/VB.NET.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Nested Gridview Bind in ASP.NET using C#/VB.NET</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).attr("src""images/minus.jpg");
            $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
          
        });
        $("[src*=minus]").live("click"function () {
            $(this).attr("src""images/plus.jpg");
            $(this).closest("tr").next().remove();
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
        <table >
            <tr>
                <td align="center">
                    <h4 style ="color:Green">Nested Gridview Bind in ASP.NET using C#/VB.NET</h4>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <asp:GridView ID="pgridview" runat="server"
                         AutoGenerateColumns="False" AllowPaging ="true"
                         OnRowDataBound="pgridview_RowDataBound" 
                         DataKeyNames="ID"CellPadding ="3">
                        <AlternatingRowStyle BackColor="pink" />
                        <HeaderStyle BackColor="#41B7D8" Font-Bold="True"ForeColor="White" />
                                             <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <img alt="" 
style="cursor: pointer" height="20px" 
width="20px"src="images/plus.jpg" />
                                    <asp:Panel ID="Panel1" Style="display: none" 
runat="server">
                                        <asp:GridView ID="ngridview" runat="server" 
CellPadding="3"
                                            AutoGenerateColumns="False"
BorderColor="black">                                                                               
                                            <HeaderStyle BackColor="#41B7D8" Font-Bold="True"ForeColor="White" />
                                            <Columns>
                                               <asp:TemplateField HeaderText="Classes">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lclasses" runat="server"Text='<%#Eval("Classes") %>'>
</asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>
                                        </asp:GridView>
                                    </asp:Panel>
                                </ItemTemplate>
                            </asp:TemplateField>                          
                            <asp:TemplateField HeaderText="FIRSTNAME">
                                <ItemTemplate>
                                    <asp:Label ID="lfname" runat="server"
Text='<%#Eval("firstname")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="LASTNAME">
                                <ItemTemplate>
                                    <asp:Label ID="llname" runat="server"
Text='<%#Eval("lastname") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="ADDRESS">
                                <ItemTemplate>
                                    <asp:Label ID="ladd" runat="server" 
Text='<%#Eval("address") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                           
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>

        </table>
    </form>
</body>
</html>

In C#.NET:
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 NestedGrid : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
    protected void  Page_Load(object sender, EventArgs e)
     {       
       
        string q = "select * from student_data";
      
        SqlDataAdapter adpter = new SqlDataAdapter(q, con);

        DataSet  dt = new DataSet();
        adpter.Fill(dt);

        pgridview.DataSource = dt;
        pgridview.DataBind();

        }

    protected void pgridview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label name= (Label)e.Row.FindControl("lfname");
            GridView gr = (GridView)e.Row.FindControl("ngridview");
            string q1 = "select id,classes from Studies where name='" + name +"'";
            SqlDataAdapter ad1 = new SqlDataAdapter(q1, con);
            DataSet dt1 = new DataSet();
            ad1.Fill(dt1);

            gr.DataSource = dt1;
            gr.DataBind();

            con.Close();
        }
    }
}

In VB.NET:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Partial Public Class NestedGrid
    Inherits System.Web.UI.Page
    Private con As New SqlConnection(
ConfigurationManager.ConnectionStrings("con").ConnectionString)
    Protected Sub Page_Load(sender As Object, e As EventArgs)

        Dim q As String = "select * from student_data"

        Dim adpter As New SqlDataAdapter(q, con)

        Dim dt As New DataSet()
        adpter.Fill(dt)

        pgridview.DataSource = dt
        pgridview.DataBind()

    End Sub

    Protected Sub pgridview_RowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim name As Label = DirectCast(e.Row.FindControl("lfname"), Label)
            Dim gr As GridView = DirectCast(e.Row.FindControl("ngridview"), GridView)
            Dim q1 As String = "select id,classes from Studies where name='" & name & "'";
            Dim ad1 As New SqlDataAdapter(q1, con)
            Dim dt1 As New DataSet()
            ad1.Fill(dt1)

            gr.DataSource = dt1
            gr.DataBind()

            con.Close()
        End If
    End Sub
End Class

The output of the above code as shown in the below figures. 

When we click on the plus button then nested Gridview will be shown.If  we click on the minus button then nested Gridview will be hide.


 "If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook page Aspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.