Link to home
Start Free TrialLog in
Avatar of Lorna70
Lorna70

asked on

onRowDataBound Foreach not going into Footer

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??
Avatar of CmdoProg2
CmdoProg2
Flag of United States of America image

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

Avatar of Lorna70
Lorna70

ASKER

Thanks but I'm using onDataBound not onRowDataBound.
ASKER CERTIFIED SOLUTION
Avatar of CmdoProg2
CmdoProg2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lorna70

ASKER

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 :-)