Solved

onRowDataBound Foreach not going into Footer

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 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