Solved

onRowDataBound Foreach not going into Footer

Posted on 2012-04-12
4
350 Views
Last Modified: 2012-08-14
Hi  
When I debug, my program is not going into the else if(gvr.RowType == DataControlRowType.Footer) part.  There is definitely a footer in my GridView and it displays on the page but for some reason my foreach doesn't recognise it - my code is as follows:

   public void GridView1_DataBound(object sender, EventArgs e)
    {

        foreach (GridViewRow gvr in gvReportonPortCatches.Rows)
        {
            if (gvr.RowType == DataControlRowType.DataRow)
            {
                //do stuff here
            }

            else if(gvr.RowType == DataControlRowType.Footer)
            {
            //do stuff here
            }
        }
    }
Please can someone advise me why this is not working as expected??
0
Comment
Question by:Lorna70
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:CmdoProg2
ID: 37842167
The onRowDataBound fires when a data row is bound to the gridview, so the whole gridview is not bound yet.  The snippet shows totaling a quantity that is in the thirdcolumn which uses a databound column definition.
 
  int totalQuantity;

 protected void grdProjects_RowDataBound(object sender, GridViewRowEventArgs e)
 {
  
  switch (e.Row.RowType)
  {
   case DataControlRowType.DataRow:
    // do your stuff here
    // add quantity that is in the third column databound (remove)
    totalQuantity += int.Parse( e.Row.Cells[2].Text);
    break;
   case DataControlRowType.Footer:
    // do your stuff here
    // Place total in Footer (remove)
    e.Row.Cells[2].Text = totalQuantity.ToString();
    break;
   case DataControlRowType.Header:
    // initialize  (remove)
    totalQuantity = 0;
    break;
   default:
    break;
  }
 }

Open in new window

0
 

Author Comment

by:Lorna70
ID: 37875874
Thanks but I'm using onDataBound not onRowDataBound.
0
 
LVL 12

Accepted Solution

by:
CmdoProg2 earned 250 total points
ID: 37891479
Sorry, but your Title and tag stated onRowDataBound.  In the databound event, you must reference the footer row through FooterRow property (gridview1.FooterRow).  The rows collection property of a gridview only contains the data rows.  When dealing with headers/footers of a grid with each row data ( as in do stuff here), I prefer the onRowDataBound event in which I may have to use a page local variable. Either design will work depending on what stuff you need to do.
0
 

Author Comment

by:Lorna70
ID: 37918098
Thanks and sorry for the wrong title (oops!).  This has worked except the row is not going into the correct place in the footer as there is already a row there and I need it to go above that row.  To be fair I'll post this problem as another question :-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now