• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

Total in Gridview Footer

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
EPSupport2
Asked:
EPSupport2
  • 6
  • 3
1 Solution
 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
put this after line 21 (your original post)

decimal rowTotal = 0;
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
HainKurtSr. System AnalystCommented:
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
 
EPSupport2Author Commented:
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
 
HainKurtSr. System AnalystCommented:
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
 
HainKurtSr. System AnalystCommented:
GridView6_RowDataBound should run each time you call GridView6.Binddata()
0
 
EPSupport2Author Commented:
Thanks for the response. I needed to add  it works now.
rowTotal = 0m;
grdTotal = 0m;
to Line 119 in  Original post.
Thanks again.
0
 
HainKurtSr. System AnalystCommented:
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
 
EPSupport2Author Commented:
Thanks for the help see above for code I need to add to make it work.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now