GridViewHelper and Nested groups in GridView

Few days ago, One of my team member has faced a problem creating nested groups with a summary of inner group's footer.

Following image shows the DataTable picture which contains the data to be bind in the grid.


protected void GroupingGridView()
{
       GridViewHelper moGrouping = new GridViewHelper(this.gvObject);

       moGrouping.RegisterGroup("Customers", true, false);
       moGrouping.RegisterGroup("Products", true, false);
       moGrouping.GroupHeader += new HeaderEvent(GroupHeaderHandler);

       moGrouping.RegisterSummary("Amount", SummaryOperation.Sum, "Products");
       moGrouping.GroupFooter += new FooterEvent(GroupFooterHandler);

       GridViewSummary moGrandSummary = moGroupingIN.RegisterSummary("Amount", SummaryOperation.Sum);
       moGrandSummary.Automatic = false;
       moGrandSummary.GeneralSummary += new SummaryEvent(GroupSummaryHandler);

}


In this method, two groups were registered "Product" and "Orders" and the expected out was something like:

Expected Output:

but every time we saw the following output until we resolved the issue.


Result Output:

See in this output, Footer of Product2 subgroup (inner group) is being created after creating the outer group i.e. Header of Customer2, which is obviously not the expected result. After investigating what was revealed that, Header event of Customer2 was being called before the Footer event of Product2, i.e.

Expected Sequence was:

Header of Customer1 event
  -  Header of  Product 1 event
  -  Footer of  Product 1 event
  -  Header of  Product 2 event
  -  Footer of  Product 2 event
Header of  Customer 2 event
  -  Header of  Product 1 event
  -  Footer of  Product 1 event
  -  Header of  Product 2 event
  -  Footer of  Product 2 event

But the output sequence was:

Header of Customer1 event
  -  Header of  Product 1 event
  -  Footer of  Product 1 event
  -  Header of  Product 2 event
Header of  Customer 2 event
  -  Footer of  Product 2 event
  -  Header of  Product 1 event
  -  Footer of  Product 1 event
  -  Header of  Product 2 event
  -  Footer of  Product 2 event


Header Event Handler:
protected void GroupHeaderHandler(GridViewGroup poGroup, GridViewRowEventArgs poEventArgs)
        {
            GridViewRow moRow;

            if (poGroup.Name == "Customers")
            {
                moRow = GridViewHelper.InsertGridRow(this.gvObject, poEventArgs.Row, 1);
                moRow.CssClass = "TRGroup1";
                moRow.Cells[0].Text =DataBinder.Eval(poEventArgs.Row.DataItem, "Customer");
            }
           else if (poGroup.Name == "Products")
            {
                moRow = GridViewHelper.InsertGridRow(this.gvObject, poEventArgs.Row, 1);
                moRow.CssClass = "TRGroup2";
                moRow.Cells[0].Text =DataBinder.Eval(poEventArgs.Row.DataItem, "Product");;
            }

         }

Footer Event Handler:
protected void GroupFooterHandlerIN(GridViewGroup poGroup, GridViewRowEventArgs poEventArgs)
        {
            GridViewRow moRow;

            if (poGroup.Name == "Products")
            {
                moRow = GridViewHelper.InsertGridRow(this.gvObject, poEventArgs.Row, 4);
                moRow.CssClass = "TRGroup3";

                moRow.Cells[2].Style["text-align"] = "right";
                moRow.Cells[2].Text = "Total";
                moRow.Cells[3].Style["text-align"] = "right";
                moRow.Cells[3].Text = poGroup.Summaries["Amount"].Value.ToString();
            }



To resolve the issue, what we have done is:

Step1: We created a single composite group.

protected void GroupingGridView()
{
       GridViewHelper moGrouping = new GridViewHelper(this.gvObject);
       
       string[] arrCols = {"Customers", "Products"};
       moGrouping.RegisterGroup(arrCols , true, false,1);

       moGrouping.GroupHeader += new HeaderEvent(GroupHeaderHandler);

       moGrouping.RegisterSummary("Amount", SummaryOperation.Sum, "Customers+Products");
       moGrouping.GroupFooter += new FooterEvent(GroupFooterHandler);

       GridViewSummary moGrandSummary = moGroupingIN.RegisterSummary("Amount", SummaryOperation.Sum);
       moGrandSummary.Automatic = false;
       moGrandSummary.GeneralSummary += new SummaryEvent(GroupSummaryHandler);

}

Step 2: Make some changes in Header and Footer Event Handlers.

Header Event Handler:
protected void GroupHeaderHandler(GridViewGroup poGroup, GridViewRowEventArgs poEventArgs)
        {
            GridViewRow moRow;

            if (poGroup.Name == "Customers+Products")
            {
               if (!poGroup.IsRepeat)

               {
                      moRow = GridViewHelper.InsertGridRow(this.gvObject, poEventArgs.Row, 1);
                      moRow.CssClass = "TRGroup1";
                      moRow.Cells[0].Text =DataBinder.Eval(poEventArgs.Row.DataItem, "Customer");
               }

              
               
GridViewRow moRow2 = GridViewHelper.InsertGridRow(this.gvObject, poEventArgs.Row, 1);
                moRow2.CssClass = "TRGroup2";
                moRow2.Cells[0].Text =DataBinder.Eval(poEventArgs.Row.DataItem, "Product");;
            }

         }

Footer Event Handler:
protected void GroupFooterHandlerIN(GridViewGroup poGroup, GridViewRowEventArgs poEventArgs)
        {
            GridViewRow moRow;

            if (poGroup.Name == "Customers+Products")
            {
                moRow = GridViewHelper.InsertGridRow(this.gvObject, poEventArgs.Row, 4);
                moRow.CssClass = "TRGroup3";

                moRow.Cells[2].Style["text-align"] = "right";
                moRow.Cells[2].Text = "Total";
                moRow.Cells[3].Style["text-align"] = "right";
                moRow.Cells[3].Text = poGroup.Summaries["Amount"].Value.ToString();
            }

After making these changes we got the expected result.

For more detail and to download GridViewHelper Class, Click here.


Comments

  1. http://taimooradilbadshah.blogspot.com.br/2013/08/gridviewhelper-and-nested-groups-in.html liked the article and want to know if you example source code. The version I have here is different from the gridview helper. My email to contact is eryssonbarros@gmail.com. thank you

    ReplyDelete
  2. I dont know the version. I have downloaded the GirdViewHelper Code from the link given in my post.

    http://www.agrinei.com/gridviewhelper/gridviewhelpersample_en.zip

    What's the version of the code you have?

    ReplyDelete
  3. Hi, could you send me your source code of this example?

    ReplyDelete

Post a Comment

Popular posts from this blog

Data Bound Controls in ASP.Net - Part 4 (FormView and DetailsView controls)

ASP.net: HttpHandlers

The Clickjacking attack and X-Frame-Options