Solved

foreach GridviewRow - skipping FooterRow

Posted on 2013-06-20
3
732 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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