Solved

onRowDataBound Foreach not going into Footer

Posted on 2012-04-12
4
356 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…

832 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