Solved

foreach GridviewRow - skipping FooterRow

Posted on 2013-06-20
3
721 Views
Last Modified: 2013-06-26
Hi

Im trying to loop through my gridview to update datarows and insert data in the footer row.
For my test I've 4 datarows to update
1 footerrows to insert
In the code below it seems to only loop through the datarows ie it goes through the loop 4 times and skips the Footer row..

 foreach (GridViewRow Row in GridView1.Rows)
       
           
        {
         
           if (Row.RowType == DataControlRowType.DataRow) // normal editable row
           {
          // update
          }
     else if (Row.RowType == DataControlRowType.Footer)
           {
          // insert
           }
   }


Any ideas?
0
Comment
Question by:louise_8
3 Comments
 
LVL 27

Assisted Solution

by:Sammy
Sammy earned 20 total points
ID: 39265628
You need to use the code in the RowDataBind event

//Something like this
void detailsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // add the UnitPrice and QuantityTotal to the running total variables
        priceTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, _
          "UnitPrice"));
        quantityTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
          "Quantity"));
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[0].Text = "Totals:";
        // for the Footer, display the running totals
        e.Row.Cells[1].Text = priceTotal.ToString("c");
        e.Row.Cells[2].Text = quantityTotal.ToString("d");
       
        e.Row.Cells[1].HorizontalAlign = _
          e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
        e.Row.Font.Bold = true;
    }
}
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 80 total points
ID: 39266508
GridView.Rows only returns the DataRows and not the Footer.
You get explicit access to footer via GridView.FooterRow property.

Check this: http://geekswithblogs.net/casualjim/archive/2005/08/29/51360.aspx
0
 

Author Closing Comment

by:louise_8
ID: 39280044
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 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