[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Total in Gridview Footer

Posted on 2010-01-06
10
Medium Priority
?
496 Views
Last Modified: 2013-11-26
I am trying to calculate a row in my Gridview and have it show the total in the footer.
I have it working I think but I have a filter that needs to be applied, If someone could have a look and point me in the right direction.
Thanks
Aspx code:

<asp:GridView ID="GridView6" runat="server" AutoGenerateColumns="False" 
            ShowFooter="True" onrowdatabound="GridView6_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Work Hours">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("CompMinuteTotal").ToString()%>'></asp:Label>
                                                
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblTotal" runat="server"></asp:Label>
                    </FooterTemplate>


Codebehind:

public partial class _Default : System.Web.UI.Page
{
    private DataSet ds;
    decimal grdTotal = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        {
            //DataSet ds = new DataSet();
            ds = new DataSet();
            string path = Server.MapPath("~/App_Data/");
            string Path = path + "timesheet.xml";
            ds.ReadXml(Path);

            GridView1.DataMember = "TransmitBatches";
            GridView2.DataMember = "Employees";
            GridView3.DataMember = "TransmitTimeSheets";
            GridView4.DataMember = "TransmitDay";
            GridView5.DataMember = "TransmitAccountDistributions";
            GridView6.DataMember = "TransmitDayCompensation";
            GridView7.DataMember = "TransmitDayCompensation";
            GridView1.DataSource = ds;
            GridView2.DataSource = ds;
            GridView3.DataSource = ds;
            GridView4.DataSource = ds;
            GridView5.DataSource = ds;
            GridView6.DataSource = ds;
            GridView7.DataSource = ds;
            GridView1.DataBind();
            GridView2.DataBind();
            GridView3.DataBind();
            GridView4.DataBind();
            GridView5.DataBind();
            GridView6.DataBind();
            GridView7.DataBind();
        }
     }
    protected void searchBtn_Click(object sender, EventArgs e)
    {
        if (ds == null) return;

        string employeeName = txtClient.Text;
        string employeeFilter = String.Format("EmployeeName LIKE '{0}%'", employeeName);
        
        DataView dv2 = new DataView( ds.Tables["Employees"], employeeFilter,"EmployeeName", DataViewRowState.CurrentRows);
        GridView2.DataSource = dv2;
        GridView2.DataBind();

        string employeeKeyFilter = "";

        DataRow[] rows = ds.Tables["Employees"].Select(employeeFilter);
        foreach (DataRow row in rows)
        {
            if (employeeKeyFilter != "") employeeKeyFilter += " AND ";
            employeeKeyFilter += String.Format("EmployeeKey = {0}", row["EmployeeKey"]);
        }

        DataView dv3 = new DataView(ds.Tables["TransmitTimeSheets"], employeeKeyFilter, "EmployeeKey", DataViewRowState.CurrentRows);
        GridView3.DataSource = dv3;
        GridView3.DataBind();
        
        //timesheet filter to filter by associated timesheet key
        string timesheetsFilter = "";

        rows = ds.Tables["TransmitTimeSheets"].Select(employeeKeyFilter);
        foreach (DataRow row in rows)
        {
            if (timesheetsFilter != "") timesheetsFilter += " AND ";
            timesheetsFilter += String.Format("TimesheetKey = {0}", row["TimesheetKey"]);
        }
        string basePayFilter = timesheetsFilter + " AND TransmitCompClassCode = 'BasePay'";
        string workHoursFilter = timesheetsFilter + " AND TransmitCompClassCode = 'WorkHours'";
        

        DataView dv4 = new DataView(ds.Tables["TransmitDay"], timesheetsFilter, "TimesheetKey", DataViewRowState.CurrentRows);
        GridView4.DataSource = dv4;
        GridView4.DataBind();
        
        DataView dv5 = new DataView(ds.Tables["TransmitAccountDistributions"], timesheetsFilter, "TimesheetKey", DataViewRowState.CurrentRows);
        GridView5.DataSource = dv5;
        GridView5.DataBind();

        DataView dv6 = new DataView(ds.Tables["TransmitDayCompensation"], workHoursFilter, "TimesheetKey", DataViewRowState.CurrentRows);
        GridView6.DataSource = dv6;
        GridView6.DataBind();
        
        
        DataView dv7 = new DataView(ds.Tables["TransmitDayCompensation"], timesheetsFilter, "TimesheetKey", DataViewRowState.CurrentRows);
        GridView7.DataSource = dv7;
        GridView7.DataBind();
    }

    protected void clearbtn_Click(object sender, EventArgs e)
    {
        txtClient.Text = "";
    }

    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }

    protected void GridView6_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            decimal rowTotal = Convert.ToDecimal
                        (DataBinder.Eval(e.Row.DataItem, "CompMinuteTotal"));
            grdTotal = grdTotal + rowTotal;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lbl = (Label)e.Row.FindControl("lblTotal");
            lbl.Text = grdTotal.ToString("c");
        }

    }

Open in new window

0
Comment
Question by:EPSupport2
  • 6
  • 3
10 Comments
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 26191194
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26191299
sure, use this
decimal rowTotal = 0;
protected void GridView6_RowDataBound(object sender, GridViewRowEventArgs e) {
	if (e.Row.RowType == DataControlRowType.DataRow) {
		rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CompMinuteTotal"));
		grdTotal = grdTotal + rowTotal;
	}
	if (e.Row.RowType == DataControlRowType.Footer) {
		Label lbl = (Label)e.Row.FindControl("lblTotal");
		lbl.Text = grdTotal.ToString("c");
	}
}

Open in new window

0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26191316
put this after line 21 (your original post)

decimal rowTotal = 0;
0
Technology Partners: 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 61

Expert Comment

by:HainKurt
ID: 26191359
sorry, ignore my posts ;)

to add a filter, do similar to this before binding to your grid...

datatable.DefaultView.RowFilter = "active=1 and ctg <> '" & myCtg "'"
0
 

Author Comment

by:EPSupport2
ID: 26191607
Hain,
Thanks for the reply. I have it calculating the amount but the problem is it it gives the whole total from the xml file, How can I apply it to calculate just the info after I have filtered the data after clicking search button?
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26191735
I could not get the problem exactly ;)

DataView dv6 = new DataView(ds.Tables["TransmitDayCompensation"], workHoursFilter, "TimesheetKey", DataViewRowState.CurrentRows);
--> add your filter here,dv6.rowfilter = "...", also set grdTotal = 0
GridView6.DataSource = dv6;
GridView6.DataBind();

and the code you already have will get the total based on your filtered data... you dont need to do anything else...
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26191745
GridView6_RowDataBound should run each time you call GridView6.Binddata()
0
 

Accepted Solution

by:
EPSupport2 earned 0 total points
ID: 26192634
Thanks for the response. I needed to add  it works now.
rowTotal = 0m;
grdTotal = 0m;
to Line 119 in  Original post.
Thanks again.
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26192721
grdTotal is already set to 0 when it is declared @ Line 21
and rowTotal is used only in GridView6_RowDataBound, you dont need to set it to 0 ;)

anyways, good to know it is working fine now...
0
 

Author Comment

by:EPSupport2
ID: 26382581
Thanks for the help see above for code I need to add to make it work.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

830 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