Solved

foreach GridviewRow - skipping FooterRow

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

623 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