Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I have a gridiview, called parent gridview, under parent one more gridview is there called as child Gridview. Under Parent gridview I have an image button called Expand/Collapse. If i click on Expand the corresponding row will expand and show the innter gridview. Suppose i open more no of inner grid's, now i want to close all the inner gridviews at a time. By using one image buttion at parent gridview header. Plz give me script idea to collapse all inner gridviews at a time. Can any body help me.

The gridview code

Sorry for the English, thanks in advance

Murthy
ASP.NET
<asp:GridView ID="grvRecords" runat="server" AutoGenerateColumns="false" Font-Bold="True"
                                    Font-Names="Trebuchet MS" Font-Size="10px" AllowPaging="True" Font-Underline="False"
                                    OnRowCommand="grvRecords_RowCommand" OnPageIndexChanging="grvRecords_PageIndexChanging"
                                    AllowSorting="True" OnSorting="grvRecords_Sorting" PageSize="10"                                     OnRowDataBound="grvRecords_RowDataBound" Width="98%" >
                                    <columns>
                                    <asp:TemplateField>
                                            <HeaderTemplate>
                                                <input id="chkAllItems" type="checkbox"  önclick="CheckAllDataGridCheckBoxes('chkItemChecked',document.forms[0].chkAllItems.checked)"
                                                    value="Check">
                                            </HeaderTemplate>
                                            <itemtemplate>
                                                <asp:CheckBox ID="chkBulk" runat="server" Width="30px" AutoPostBack="False">
                                            </itemtemplate>
                                        
                                       <asp:TemplateField ItemStyle-Width="20px">
                                            <itemtemplate>
                                                <asp:Image runat="server" ID="img1" ImageUrl="images/Expand.GIF" />
                                            </itemtemplate>
                                        
                                        
                                        
                                        <asp:TemplateField Visible="false">
                                            <itemtemplate>
                                                <asp:Label ID="lblCodingId" runat="server" Text='<%#Eval("iCodingId") %>'>
                                            </itemtemplate>
                                        
                                                                                <asp:BoundField DataField="UploadDate" HeaderText="UploadDate" SortExpression="UploadDate" />
                                        <asp:BoundField DataField="vcRequest" HeaderText="Request" />
                                        <asp:BoundField DataField="vcStudentName" HeaderText="PName" />
                                                                                <asp:BoundField DataField="DOB" HeaderText="DOB" />
                                        <asp:BoundField DataField="DOJ" HeaderText="JOin.Date" />
                                        <asp:BoundField DataField="Course" HeaderText="Course" />
                                        <asp:TemplateField HeaderText="Category">
                                            <itemtemplate>
                                                <asp:Label ID="lblCategory" runat="server" Text='<%# Eval("Category")%>'>
                                                <asp:Literal runat="server" ID="lit1" Text="<tr id="trCollapseGrid" style="display:none"><td colspan="2"><td colspan="8" align="right">" />
                                                <asp:GridView ID="gvChild" AutoGenerateColumns="False" runat="server" EnableViewState="False" Width="100%"
                                                    ForeColor="#006666" HeaderStyle-HorizontalAlign="Center"  >
                                                    <rowstyle backcolor="#EFF3FB" />
                                                    <alternatingrowstyle backcolor="#EFF3FB" />
                                                    <columns>
                                                                                             <itemtemplate>
                                                        <asp:LinkButton ID="lnk1" runat ="server" Text='<%#Eval("RegNo") %>' CommandName="RegNo" CommandArgument='<%#Eval("RegNo") %>' >
                                                        </itemtemplate>
                                                        --%>
                                                        <asp:BoundField DataField ="vcCPT" HeaderText ="CPT" ItemStyle-HorizontalAlign="Center" />
                                                        <asp:BoundField HeaderText="Address1" DataField="Address1" ItemStyle-HorizontalAlign="Center" >
                                                        
                                                        <asp:BoundField HeaderText="Address2" DataField="Address2" ItemStyle-HorizontalAlign="Center" >
                                                        
                                                        <asp:BoundField HeaderText="Phone1" DataField="Phone1" ItemStyle-HorizontalAlign="Center" >
                                                        
                                                        <asp:BoundField HeaderText="Phone2" DataField="Phone2" ItemStyle-HorizontalAlign="Center" >
                                                        
                                                       <HeaderText="Remarks" DataField="vcRemarks" ItemStyle-HorizontalAlign="Center"  >
                                                        
                                                    </columns>
                                                    <HeaderStyle BackColor="#82c0ff" Font-Bold="True" ForeColor="White" />
                                                
                                                <asp:Literal runat="server" ID="lit2" Text="</td></td></tr>" />
                                            </itemtemplate>
                                        
                                    </columns>
                                    <HeaderStyle Font-Bold="True" Font-Underline="False" ForeColor="#0033CC" BackColor="#6699FF"
                                        Font-Names="Trebuchet MS" Font-Size="12px" />


C# code
C#
protected void grvRecords_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
        if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataTable dtChildTable = BindChildGridview();
                DataTable dtCloneChildTable = dtChildTable.Clone();
                //Image img = (Image)e.Row.Cells[1].FindControl("img1");
                Image img = (Image)e.Row.FindControl("img1");
                Literal ltrl = (Literal)e.Row.FindControl("lit1");
                ltrl.Text = ltrl.Text.Replace("trCollapseGrid", "trCollapseGrid" + e.Row.RowIndex.ToString());
                string str = "trCollapseGrid" + e.Row.RowIndex.ToString();
                e.Row.Cells[1].Attributes.Add("OnClick", "OpenGridTable('" + str + "','" + img.ClientID + "')");
                e.Row.Cells[1].RowSpan = 1;
                System.Web.UI.WebControls.GridView gvChild = (System.Web.UI.WebControls.GridView)e.Row.FindControl("gvChild");
                Label lbliPatId = (System.Web.UI.WebControls.Label)e.Row.FindControl("lbliPatientId");
                //DataRow[] gvChildRows = dtChildTable.Select("iPatientId = " + e.Row.Cells[0].Text);
                DataRow[] gvChildRows = dtChildTable.Select("iPatientId = " + Convert.ToInt32(lbliPatId.Text));
                foreach (DataRow gvChildRow in gvChildRows)
                {
                    dtCloneChildTable.ImportRow(gvChildRow);
                }
                gvChild.DataSource = dtCloneChildTable;
                gvChild.DataBind();
            }
        }

Script for Expand individual inner gridview
JavaScript
<script language="javascript" type="text/javascript">
        //debugger;
        function OpenGridTable(trRow, imgId) {
            object = document.getElementById(trRow);
            if (object.style.display == "none") {
                object.style.display = "";
                document.getElementById(imgId).src = "Images/Collapse.gif";
            }
            else {
                object.style.display = "none";
                document.getElementById(imgId).src = "Images/Expand.gif";
            }
        }
    </script>
Posted
Updated 10-Jul-12 0:23am
v2

1 solution

Hi,
Set the command name to the buttion at parent gridview header as "CollapsAll". Handle "CollapsAll" command as follows:
C#
protected void gvEmpDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "CollapsAll")
        {
            fnCollapsAll();
        }
    }

Now in fnCollapsAll set the all child grid-view data source to null as follows:

C#
private void fnCollapsAll(){
    foreach(DataRow row in grvRecords.Rows){
          GridView gv=(GridView)row.FindControl("gvChild");
          gv.DataSource=null;
          gv.DataBind();
          //If you don't want to set the datasource as null just make it 
          //gv.Visible = false;
    }
}


All the best.
--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900