Solved

C#

Posted on 2008-09-29
6
207 Views
Last Modified: 2012-05-05
I want to add the values in the column and place the result in the gridview footer row. I'm using below code but its getting fired before i fill the gridview and going into header row if statement.
Could you please tell me which event to use to get the values in the footer row.
 
Note: The values i'm adding are in the label inside the gridview. I need to manually enter the values inside the gridview and perform calculations.

protected void GridViewHW_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView tabledata = e.Row.DataItem as DataRowView;
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TotalHWCost = 0;
        }
        else if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TotalHWCost += Convert.ToInt16(tabledata[3]);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label TotalHWCostref = e.Row.FindControl("LabelTotalHardwareCost") as Label;
            TotalHWCostref.Text = "$"+TotalHWCost.ToString();
        }

    }
0
Comment
Question by:MRManthani
  • 3
  • 3
6 Comments
 
LVL 5

Expert Comment

by:johnaryan
ID: 22597661
There is an OnRowAdded event that you could use....
0
 

Author Comment

by:MRManthani
ID: 22597932
I didnt find any onrowadded event but i can view onrowcommand,onrowcreated...Can I you any of them??If so please send the code how to take control of labels inside the grdivew.
0
 

Author Comment

by:MRManthani
ID: 22597944
Can I use any of them OnRowCommand/OnRowCreated??
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:johnaryan
ID: 22599590
They are both used slightly differently.
OnRowCommand is used when a command button is used to update the row. The OnrowCreated fires when a new row is added.
Here's a goog article on how and when to use both:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowcreated.aspx

As long as you have the given the Labels id's in the row, you be able to reference them using code like:
Label label = (Label)e.Row.FindControl("myLabel");

0
 

Author Comment

by:MRManthani
ID: 22603598
Now how should i proceed to perform calculations in the column and get the result in the footer row.
0
 
LVL 5

Accepted Solution

by:
johnaryan earned 500 total points
ID: 22603813
You can add the footer row to the table definition and then call a function to return the calculated totals:
Your OnRowCreated can can record the total in a Property and then the call to GetTotal() will return that Property value.

Reference:
http://aspalliance.com/782_CodeSnip_How_to_Display_Sum_Total_in_the_Footer_of_the_GridView_Control
<asp:GridView ID="GridView1"
 ...
<Columns>
...
<FooterTemplate>
  <%# GetTotal().ToString() %>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

680 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