Home » VB.NetRSS

Gridview Subtotals and totals

my table has div code, inventory date, landfill name , no of tonnes and total tonnes for a particular division and month tht the user selects

i need to display data such as show below as a sample:-

Division Month   day Landfill Name No Of Loads Total tonnes
000        August  1     Niagara            10                   5
000        August  2     Niagara            10                   5
000        August  3     Niagara            10                   5
000        August  4     Niagara            10                   5
000        August  5     Niagara            10                   5
000        August  6     Niagara            10                   5
000        August  7     Niagara            10                   5
--------------------------------------------------------------------------
Week Total                                         70                  35
--------------------------------------------------------------------------
000        August   8     Niagara            10                   5
000        August   9     Niagara            10                   5
000        August  10     Niagara            10                   5
000        August  11     Niagara            10                   5
000        August  12    Niagara            10                   5
000        August  13     Niagara            10                   5
000        August  14     Niagara            10                   5
--------------------------------------------------------------------------
Week Total                                         70                  35
--------------------------------------------------------------------------

 and so on for weekly also a grand total for all weeks after the last week of the current landfill

and then for the next landfill do the same weekly totals

and then grand totals

Can anyone help me how

 

 

 

 

 

5 Answers Found

 

Answer 1

for that you need to create DataTable and add row one by one with the data  into DataTable and assign to Grid.

Based on you requirement you must be made logic for this.


 

Answer 2

Thanks MItesh

I do appreciate you helping me

I solved the above requirement with a Gridview..................... 

Michelle

 

 

Answer 3

You probably have the data  at RowDataBound or RowCreated event.  Add each number to two totals  (one for weekly, one for grand).

When gridview  displays, put these totals in labels - done.

 

Answer 4

You can do it with LINQ like this


var records = new[] { new { month  = "August",day = "1", Tonnes = 5 } }.ToList();
//Test Records for a month
for (int i = 2; i < 31; i++)
    records.Add(new { Month = "August", day  = i.ToString(), Tonnes = 5 });
//Select week dates and calculate running total
// Add grandTotal after every week
records.Where(a => new string[] { "7", "14", "21", "28" }.Contains(a.day)).ToList().
                    ForEach(x => records.Insert(records.IndexOf(x) + 1, new
                    {
                        Month = "Week Total",
                        day = "",
                        Tonnes = records.Where(z => z.day != "").
                                            Where(a => Convert.ToInt32(a.day) >= Convert.ToInt32(x.day) - 6 &&
                                                    Convert.ToInt32(a.day) <= Convert.ToInt32(x.day))
                                        .Sum(b => b.Tonnes)
                    }));
GridView2.DataSource = records;
GridView2.DataBind();
//Highlight GrandTotal Rows
GridView2.Rows.Cast<GridViewRow>().Where(a => a.Cells[0].Text == "Week Total").
                ToList().
                ForEach(a => a.BackColor = Color.FromName("#E2DED6"));
 

Answer 5

Perhaps you don't image that codes will be so easy, just call this:
protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper = new GridViewHelper(this.GridView1);
    helper.RegisterGroup("ShipRegion", true, true);
    helper.RegisterGroup("ShipName", true, true);
    helper.ApplyGroupSort();
}

do you want to know how it functions?
Please download the sample  here:
http://www.agrinei.com/gridviewhelper/gridviewhelpersample_en.zip

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter