SIGN UP MEMBER LOGIN:    
ARTICLE

Collapsible GridView in ASP.Net 2.0

Posted by Raghavendra u Articles | ASP.NET Programming September 27, 2010
This article explains how to create a collapsible and expandable gridview in asp.net 2.0 using javascript.
Reader Level:

This article explains how to create a collapsible and expandable gridview in asp.net 2.0 using javascript.

Add this script below to the title in head section. 


<
script type="text/javascript">

    function OnImageClick(Obj,PnlId)
    {
        var Src=Obj.src.split("/")[Obj.src.split("/").length-1];
        if(Src=="Down.png")
        {
            document.getElementById(Obj.id).src="Images/Up.png";
            document.getElementById(Obj.id).title="Expand";
            document.getElementById(PnlId).style.display="block";
        }
        else if(Src=="Up.png")
        {
            document.getElementById(Obj.id).src="Images/Down.png";
            document.getElementById(Obj.id).title="Collapse";
            document.getElementById(PnlId).style.display="none";
        }
    }

</
script>

Drag and drop a gridview to your webform and design like this, 


<asp:GridView ID="GVParent" AutoGenerateColumns="false" HorizontalAlign="center" runat="server" OnRowDataBound=" GVParent_RowDataBound" BorderColor="WhiteSmoke" BorderStyle="Solid" BorderWidth="1px">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <table border="0" cellpadding="1" cellspacing="0">
                                <tr style="background-color:#4682b4; color:White">
                                    <td align="center" style="width: 20px" valign="top">
                                    </td>
                                    <td align="center" style="width: 75px" valign="top">
                                        Regno</td>
                                    <td align="left" style="width: 300px" valign="top">
                                        Student Name</td>
                                    <td align="center" style="width: 75px" valign="top">
                                        Total</td>
                                </tr>
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table border="0" cellpadding="1" cellspacing="0">
                                <tr>
                                    <td align="center" style="width: 20px" valign="top">
                                        <asp:Image ID="Img" ImageUrl="~/Images/Down.png" runat="server" />
                                    </td>
                                    <td align="center" style="width: 75px" valign="top">
                                        <asp:Label ID="lblRegno" runat="server" Text='<%# Eval("Regno") %>'></asp:Label>
                                    </td>
                                    <td align="left" style="width: 300px" valign="top">
                                        <%# Eval("Name") %>
                                    </td>
                                    <td align="center" style="width: 75px" valign="top">
                                        <%# Eval("Total") %>
                                    </td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td align="left" colspan="3" valign="baseline" style="padding-left: 25px">
                                        <asp:Panel ID="PnlChild" runat="server" style="display:none">
                                            <asp:GridView ID="GVChild" runat="server" AutoGenerateColumns="false" Width="300" BorderColor="WhiteSmoke"
BorderStyle="Solid" BorderWidth="1px">
                                                <EmptyDataTemplate>
                                                    <asp:Label ID="Label1" runat="server" Text="No Data to Display" Font-Bold="true"
                                                        Font-Names="Calibri" ForeColor="red"></asp:Label>
                                                </EmptyDataTemplate>
                                                <Columns>
                                                    <asp:TemplateField HeaderText="Slno">
                                                        <ItemTemplate>
                                                            <%# Container.DataItemIndex + 1 %>
                                                        </ItemTemplate>
                                                        <ItemStyle HorizontalAlign="Center" Width="50px" />
                                                    </asp:TemplateField>
                                                    <asp:BoundField DataField="Subject" HeaderText="Subject" />
                                                    <asp:BoundField DataField="TestName" HeaderText="Test" >
                                                        <ItemStyle HorizontalAlign="Center" />
                                                    </asp:BoundField>
                                                    <asp:BoundField DataField="Marks" HeaderText="Marks" NullDisplayText="0" >
                                                        <ItemStyle HorizontalAlign="Center" />
                                                    </asp:BoundField>
                                                </Columns>
                                                <RowStyle BackColor="White" />
                                                <HeaderStyle BackColor="SteelBlue" ForeColor="White" />
                                                <AlternatingRowStyle BackColor="WhiteSmoke" />
                                            </asp:GridView>
                                        </asp:Panel>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <RowStyle Font-Names="Calibri" />
                <HeaderStyle Font-Names="Calibri" />
                <AlternatingRowStyle BackColor="WhiteSmoke" Font-Names="Calibri" />
            </asp:GridView>


Bind the parent gridview. 


protected
void Page_Load(object sender, EventArgs e)
{

try

      {
            if (!IsPostBack)
            {
                  BindParentGrid(); //bind parent gridview with your data.
            }
      }
      catch (Exception Ex)
      { Response.Write(Ex.Message); }
}

Now bind the child gridview in parent gridview RowDataBound event.

protected void GVParent_RowDataBound(object sender, GridViewRowEventArgs e)
{

      try

      {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblRegno = e.Row.Cells[0].FindControl("lblRegno") as Label;
                Panel PnlChild = e.Row.Cells[0].FindControl("PnlChild") as Panel;
                GridView GVChild = e.Row.Cells[0].FindControl("GVChild") as GridView;
                Image Img = e.Row.Cells[0].FindControl("Img") as Image;

                Img.Attributes.Add("onclick", "OnImageClick(this,'" + PnlChild.ClientID + "')");
                  BindChildGrid(lblRegno.Text); // here passing student regno as parameter to fill child grid
            }
      }
      catch (Exception Ex)
      { Response.Write(Ex.Message); }
}

Images to download

img.gif

Thanks,

Raghavendra
 

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks. I will try it

Posted by Raghavendra u Dec 01, 2011

Hi Raghavendra, Thanks for your solution. Upon selecting a new page from child grid it's getting collapsed, is there an option to stay expanded. Saradhi

Posted by Saradhi Mudigonda Nov 30, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor