David Adlington
David Adlington

Reputation: 666

ASP.Net Grid View rolling total in Gridview

I have seen several tutorials on how to achieve this.

However in my opinion they require a lot of prior knowledge on how to programatically refer to each item.

Does anyone have a link to or can create a relatively basic example of how to achive a running total in the footer for an ASP:Gridview?

Upvotes: 1

Views: 1887

Answers (4)

Eric Barr
Eric Barr

Reputation: 4165

I think the method I use is pretty basic and doesn't require programatically referring to columns in the Gridview, if that's what you mean. That's one of the nice parts is that once you get the back-end functions written, you can add totals to any Gridview by only editing the .aspx file.

In your GridView, make the column like this:

<asp:TemplateField HeaderText="Hours">
    <ItemTemplate><%#DisplayAndAddToTotal(Eval("Hours").ToString(), "Hours")%></ItemTemplate>
    <FooterTemplate><%#GetTotal("Hours")%></FooterTemplate>
</asp:TemplateField>

The second parameter to DisplayAndAddToTotal can be any string you want as long as you use the same string in GetTotal. I usually just use the field name again though. Here are the two functions used, DisplayAndAddToTotal and GetTotal. They use a Hashtable to store the totals so that it works with any number of columns you want to add up. And they also work with counting the number of "True"s for a Boolean field.

Protected total As Hashtable = New Hashtable()

Protected Function DisplayAndAddToTotal(itemStr As String, type As String) As Double
    Dim item As Double

    If itemStr = "True" Then
        item = 1

    ElseIf Not Double.TryParse(itemStr, item) Then
        item = 0
    End If

    If total.ContainsKey(type) Then
        total(type) = Double.Parse(total(type).ToString()) + item
    Else
        total(type) = item
    End If

    Return item
End Function

Protected Function GetTotal(type As String) As Double
    Try
        Dim result As Double = Double.Parse(total(type).ToString())
        Return result
    Catch
        Return 0
    End Try
End Function

Upvotes: 0

briskovich
briskovich

Reputation: 690

This is how I do it. Very easy. You just sum the row that has your numbers in it and place it in the footer.

((Label)GridView.FooterRow.Cells[1].FindControl("your_label")).Text = ds.Tables[0].Compute("sum(Column_name)", "").ToString();

Upvotes: 0

Novice
Novice

Reputation: 913

Add the footer Template and on the RowDataBound, have a global variable to store the summation sum,

At the e.Row.RowType = DataControlRowType.DataRow type do the summation , and @ the e.Row.RowType = DataControlRowType.Footer store the vale in the appropriate cell

for further info look @ MSDN LINK

Upvotes: 0

JohnOpincar
JohnOpincar

Reputation: 5823

This is what I use:

protected void InvoiceGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    var invoice = (Invoice) e.Row.DataItem;
    if (e.Row.RowType == DataControlRowType.Header)
    {
        totalAmt = 0;
    }
    else if (e.Row.RowType == DataControlRowType.DataRow)
    {
        totalAmt += invoice.Amount;
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        var amountTotalLabel = (TextBox) e.Row.FindControl("AmountTotalTextBox");
        amountTotalLabel.Text = totalAmt.ToString("0.00");
    }
}

TotalAmt is protected instance variable on the page. Not sure if it's what you were looking for based on your comment about "programmatic knowledge." But it works and is fairly straight-forward. The gridview is bound to a List<Invoice> in this case.

Upvotes: 2

Related Questions