Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. Is it possible to sum up a column in gridview BUT only getting value of unique Date column

Say, I have these values:

____________________
|   Date    | Value |
| 03/01/16  |   12  |
| 03/02/16  |   6   |
| 03/02/16  |   6   |
| 03/03/16  |   10  |



Total should be 12 +6+10 = 28
03/02/16 will not be summed up but will only get the value of one since the data repeats.

Regards

What I have tried:

I tried using gridview OnRowDataBound but it sums up all.

C#
public Decimal TotalValue=0;
protected void gvLevel3OT_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TotalValue+= Convert.ToDecimal(e.Row.Cells[5].Text.ToString());
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[3].Text = "Total";
        e.Row.Cells[5].Text = TotalValue.ToString("F");
    }
}
Posted
Updated 5-Apr-16 17:36pm
v5
Comments
dan!sh 5-Apr-16 23:09pm    
Is this data coming from a database?
bjay tiamsic 6-Apr-16 2:46am    
Yes sir. After I bind it to the gridview, i will get the total
dan!sh 6-Apr-16 3:18am    
Can't you add a group by clause in the query? You will get what you need straight from database.

1 solution

Before adding value, compare the date in the cell with dates other rows, except current row. You can do by a simple loop or linq. If found, don't add.
 
Share this answer
 
Comments
bjay tiamsic 6-Apr-16 2:47am    
isn't it complicated and wil cause more loop since it has to go through every row every time i check for the value?
Then directly manipulate while getting data from database in the query itself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900