title: Add sorting to a Nested Gridview ASP.net with C#
body: <p>I have searched high and low for how to implement this on my page. I have a top level gridview which allows sorting and nested gridviews which are dynamically generated when the page compiles so 'x' number of nested gridviews which are inside dynamic html divs which can be toggled from invisible to visible at the users command. The problem I have is that I cannot figure out how to allow sorting on these nested gridviews without collapsing the divs/causing a postback.</p>
  
  <p>Below shows how the master gridview (gvSalesDiv) and the nested gridview (gvTheDivisionCustomers) are generated in asp.net </p>
  
  <pre><code>    &lt;asp:GridView ID="gvSalesDiv" AllowSorting="true" onsorting="GridView1_Sorting" runat="server" GridLines="Both" OnRowDataBound="gvOrderLineDetail_RowDataBound" AutoGenerateColumns="False"
          Width="100%" Height="210px" BackColor="WhiteSmoke" AlternatingRowStyle-BackColor="#DADDE2"
          HeaderStyle-Font-Size="Medium"  Visible="true"&gt;
          &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="Toggle Detail"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;a href="javascript:switchViews('div&lt;%# Eval("SalesDivision") %&gt;');"&gt;
  
                          &lt;img id="imgdiv&lt;%# Eval("SalesDivision") %&gt;" alt="toggle"  border="0"
                           src="/salesconsole/toggle-off.png" /&gt;
                          &lt;/a&gt;                      
  
                      &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
  
          &lt;asp:BoundField DataField="SalesDivision"  HeaderText="Sales Division"&gt;
  
                  &lt;ItemStyle  Font-Bold="True" ForeColor="CornflowerBlue" HorizontalAlign="Center"&gt;&lt;/ItemStyle&gt;
                  &lt;/asp:BoundField&gt;
                  &lt;asp:BoundField DataField="LastDay" SortExpression="LastDay" DataFormatString="{0:C}" HeaderText="Last 24 Hours" &gt;
  
  
                  &lt;/asp:BoundField&gt;
                  &lt;asp:BoundField DataField="LastWeek" SortExpression="LastWeek" DataFormatString="{0:C}" HeaderText="Last 7 Days" &gt;
  
                  &lt;/asp:BoundField&gt;
                  &lt;asp:BoundField DataField="Last30Days" SortExpression="Last30Days" DataFormatString="{0:C}" HeaderText="Last 30 Days" &gt;
  
                  &lt;/asp:BoundField&gt;
                  &lt;asp:BoundField DataField="Last3Months" SortExpression="Last3Months" DataFormatString="{0:C}" HeaderText="Last 3 Months" &gt;
  
                  &lt;/asp:BoundField&gt;
                  &lt;asp:BoundField DataField="Last6Months" SortExpression="Last3Months" DataFormatString="{0:C}" HeaderText="Last 6 Months" &gt;
  
                  &lt;/asp:BoundField&gt;
                  &lt;asp:BoundField DataField="LastYear" SortExpression="LastYear" DataFormatString="{0:C}" HeaderText="Last Year" &gt;
  
                  &lt;/asp:BoundField&gt;
                  &lt;asp:TemplateField&gt;
                      &lt;ItemTemplate&gt;
                                   &lt;tr&gt;
                                   &lt;td colspan="100"&gt;     
                               &lt;div id="div&lt;%# Eval("SalesDivision") %&gt;" style="display:none;position:relative;left:25px;" &gt;
  
           &lt;h3 title="&lt;%# Eval("SalesDivision") %&gt; Sales"&gt;&lt;%# Eval("SalesDivision") %&gt; Sales Breakdown&lt;/h3&gt;
  
          &lt;asp:GridView ID="gvTheDivisionCustomers" AllowSorting="true" onsorting="GridView2_Sorting" BackColor="WhiteSmoke" AlternatingRowStyle-BackColor="#DADDE2"
            Width="100%" 
          AutoGenerateColumns="false" runat="server"&gt;
          &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="Show More Detail"&gt;
                      &lt;ItemTemplate&gt;
                         &lt;a href="sales-customers-detail.aspx?CustomerID=&lt;%# Eval("CustomerID") %&gt;&amp;CustomerName=&lt;%# Eval("CustomerName") %&gt;" target="_blank" style="color:Blue; text-decoration:underline"&gt; More Details
                          &lt;/a&gt;                      
  
                      &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
  
          &lt;asp:BoundField DataField="CustomerID" HeaderText="ID"/&gt;
          &lt;asp:BoundField DataField="CustomerName" HeaderText="Name"  /&gt;
          &lt;asp:BoundField DataField="Last24Hours" HeaderText="Last 24 Hours" SortExpression="LastDay" DataFormatString="{0:C}"  /&gt;
          &lt;asp:BoundField DataField="Last7Days" HeaderText="Last 7 Days" SortExpression="Last7Days" DataFormatString="{0:C}"  /&gt;
         &lt;asp:BoundField DataField="Last30Days" HeaderText="Last 30 Days" SortExpression="Last30Days" DataFormatString="{0:C}"  /&gt;
         &lt;asp:BoundField DataField="Last3Months" HeaderText="Last 3 Months" SortExpression="Last3Months" DataFormatString="{0:C}"  /&gt;
         &lt;asp:BoundField DataField="Last6Months" HeaderText="Last 6 Months" SortExpression="Last6Months" DataFormatString="{0:C}"  /&gt;
         &lt;asp:BoundField DataField="LastYear" SortExpression="LastYear" HeaderText="Last Year" DataFormatString="{0:C}" /&gt;
  
         &lt;/Columns&gt;
  
  
          &lt;/asp:GridView&gt;
  
                                 &lt;/div&gt;    
                                   &lt;/td&gt;&lt;/tr&gt;    
                                &lt;/ItemTemplate&gt;
  
                                              &lt;/asp:TemplateField&gt;  
          &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>I populate the master gridview on Page_Load and the nested gridviews are created using the OnRowDataBound method. I have a sorting method for the master gridview which works fine as well. Below is the OnSorting method for the nested gridview, which is where I am stuck...I cannot access this object</p>
  
  <pre><code>    protected void GridView2_Sorting(Object sender, GridViewSortEventArgs e)
  {
      // TO DO : Sort the nested gridview....All I can get at is the sort expressions or
      cast the sender into a gridview but even then I wouldn't know the correct SQL query to bind with unless I knew which 'div' I was in... 
  
  }
  </code></pre>