Solved

onRowDataBound Foreach not going into Footer

Posted on 2012-04-12
4
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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