Solved

C#

Posted on 2008-09-29
6
203 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
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.

 
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

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

13 Experts available now in Live!

Get 1:1 Help Now