title: Improving Grouping Speed in Gridview
body: <p>I have a web form where I need to group items/rows based on some criteria. It has multilevel grouping and that is why rendering items in the gird becomes very tedious process for the server.</p>
  
  <p>Here is what I do currently. (This is for only 3 level).</p>
  
  <pre><code>protected void ResultGrid_PreRender(object sender, EventArgs e)
  {
      foreach (GridViewRow gdR in ResultGrid.Rows)
      {
          Label lblClass = (Label)gdR.FindControl("lblClass");
          Label lblCategory = (Label)gdR.FindControl("lblCategory");
          Label lblCompartment = (Label)gdR.FindControl("lblCompartment");
          Panel pnlClassLinks = (Panel)gdR.FindControl("pnlClassLinks");
  
          foreach (GridViewRow gdRIn in ResultGrid.Rows)
          {
             if (gdRIn.RowIndex != gdR.RowIndex)
             {
               Label lblClassIn = (Label)gdRIn.FindControl("lblClass");
               Label lblCategoryIn = (Label)gdRIn.FindControl("lblCategory");
               Label lblCompartmentIn = (Label)gdRIn.FindControl("lblCompartment");
  
               if(lblClassIn.Text == lblClass.Text)
                 {
                     if(lblCategoryIn.Text == lblCategory.Text)
                     {
                         if(lblCompartmentIn.Text == lblCompartment.Text)
                         {
                             LinkButton lnkBtn = new LinkButton();
                             // lnkBtn Properties added
                             pnlClassLinks.Controls.Add(lnkBtn);
                             if (pnlClassLinks.Controls.Count &gt; 2)
                             {
                                 pnlClassLinks.Width = 150;
                             }
                             if (gdR.Visible)
                             {
                                 dr.Visible = false;
                             }
                         }
                     } 
                 }
             }
          }
        LinkButton lnkGroupEdit = (LinkButton)gdR.FindControl("lnkGroupEdit");
        lnkGroupEdit.OnClientClick = "editGroup();";
      }
  }
  </code></pre>
  
  <p>As it clearly shows, the rows iteration is too much, when the number of rows in gridview increases. So, is there any better way to do this?</p>