Monday, December 5, 2011

Expandable Rows In GridView [ GridView In GridView ]


This article shows how to add functionality to the ASP.NET GridView control to allow the display of master/detail records with expanding rows. It probably isn't suited to scenarios where large number of records will be returned at a time.
This example is based on work I did for an equine hospital showing appointments where the cost of treatments had exceeded the limit agreed with the client. Expanding the details lists the transactions which make up the cost.

Default.aspx

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                <ContentTemplate>
                                    <asp:GridView ID="DataDisplay" runat="server" Width="100%" AutoGenerateColumns="false"
                                        AllowPaging="True" CssClass="GridViewStyle" EnableViewState="False" PageSize="50">
                                        <EmptyDataTemplate>
                                            <span lang="en-us">Enter part #</span>
                                        </EmptyDataTemplate>
                                        <Columns>
                                            <asp:BoundField DataField="islast" HeaderText="S No" />
                                            <asp:BoundField DataField="ClosingOnId" HeaderText="Closing On ID" />
                                            <asp:BoundField DataField="MemberName" HeaderText="Member Name" />
                                            <asp:BoundField DataField="PaidDate" HeaderText="Paid Date" />
                                            <asp:BoundField DataField="PayoutType" HeaderText="Payout Type" />
                                            <asp:TemplateField>
                                                <ItemTemplate>
                                                    <a href="javascript:switchViews('<%# Eval("islast") %>', 'one');">
                                                        <%--  <img id="imgdiv<%# Eval("Productid") %>" alt="Click to show/hide orders" height="30" width="30" border="0" src="images/control_down.png" />--%>
                                                        View Payment Details Report </a>
                                                </ItemTemplate>
                                                <AlternatingItemTemplate>
                                                    <a href="javascript:switchViews('<%# Eval("islast") %>', 'alt');">
                                                        <%--  <img id="imgdiv<%# Eval("Productid") %>" alt="Click to show/hide orders" height="30" width="30" border="0" src="images/control_down.png" />--%>
                                                        View Payment Details Report </a>
                                                </AlternatingItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField>
                                                <ItemTemplate>
                                                    </td></tr>
                                                    <tr>
                                                        <td colspan="100%">
                                                            <div id="<%# Eval("islast") %>" style="display: none; position: relative; left: 25px;">
                                                                <asp:GridView ID="GridView2" runat="server" Width="80%" AutoGenerateColumns="false"
                                                                    DataKeyNames="memberid" EmptyDataText="This No Member In this  ">
                                                                    <HeaderStyle CssClass="dataTable" />
                                                                    <AlternatingRowStyle CssClass="dataTableAlt" />
                                                                    <RowStyle CssClass="dataTable" />
                                                                    <Columns>
                                                                        <asp:BoundField DataField="memberid" HeaderText="Member ID" SortExpression="memberid" />
                                                                        <asp:BoundField DataField="MemberName" HeaderText="Member Name" SortExpression="MemberName" />
                                                                        <asp:BoundField DataField="pairs" HeaderText="Binary Pairs" SortExpression="PurchaseDate" />
                                                                        <asp:BoundField DataField="binaryincome" HeaderText="Binary Income" SortExpression="ProductName" />
                                                                        <asp:BoundField DataField="tds" HeaderText="TDS" SortExpression="Qty" />
                                                                        <asp:BoundField DataField="admin" HeaderText="Admin" SortExpression="Qty" />
                                                                    </Columns>
                                                                </asp:GridView>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                        <RowStyle CssClass="RowStyle" />
                                        <PagerStyle CssClass="PagerStyle" />
                                        <HeaderStyle CssClass="HeaderStyle" />
                                        <AlternatingRowStyle CssClass="AltRowStyle" />
                                        <PagerSettings Position="TopAndBottom" />
                                    </asp:GridView>
                                </ContentTemplate>
                            </asp:UpdatePanel>
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TCConnection %>"
                                SelectCommand="select row_number() over(order by m.msrno) sno,P.MsrNo,m.memberid,m.membername,p.pairs,p.binaryincome,p.tds,p.admin FROM dbo.payment AS p INNER JOIN dbo.MemberMaster  AS m ON p.msrno = m.msrno where chno>0 order by MsrNo"
                                ProviderName="<%$ ConnectionStrings:TCConnection.ProviderName %>"></asp:SqlDataSource>
...............................................



<script type="text/javascript" language="javascript">
        function switchViews(obj, row) {
            var div = document.getElementById(obj);


            //var img = document.getElementById('imgdiv' + obj);




            if (div.style.display == "none") {
                div.style.display = "inline";
                //            if (row == 'alt') {
                //                img.src = "images/control_up.png"; mce_src = "images/control_up.png";
                //            }
                //            else {


                //                img.src = "images/control_up.png"; mce_src = "images/control_up.png";


                //            }


            }
            else {
                div.style.display = "none";
                ////            if (row == 'alt') {
                ////                img.src = "images/control_down.png"; mce_src = "images/control_down.png";
                ////            }
                ////            else {
                ////                img.src = "images/control_down.png"; mce_src = "images/control_down.png";
                ////            }


            }
        }
    
    </script>



 Protected Sub DataDisplay_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles DataDisplay.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim gvv As GridView = e.Row.FindControl("GridView2")
            Dim sda As SqlDataAdapter
            Dim dt As DataSet = New DataSet()
            Dim tcon As SqlConnection = New SqlConnection()
            tcon.ConnectionString = ConfigurationManager.ConnectionStrings("TCConnection").ConnectionString
            'If tcon.State == ConnectionState.Closed Then 
            tcon.Open()
            'End If
            If e.Row.Cells(3).Text = "" Or e.Row.Cells(3).Text = "&nbsp;" Then
            Else


                Dim value As String = e.Row.Cells(3).Text.Trim().ToString()
                Dim str As String = "select * from test where islast='" & e.Row.Cells(0).Text.Trim().ToString() & "' "
                sda = New SqlDataAdapter(str, tcon)
                sda.Fill(dt)


                gvv.DataSource = dt.Tables(0)
                gvv.DataBind()
            End If
            tcon.Close()
        End If
    End Sub



No comments :

Post a Comment