简体   繁体   中英

What is the best way to create a GridView showing summary sales data?

I have developed an eCommerce application in C# and ASP.Net. For the Admin users "dashboard" landing page, I would like to give them a GridView that shows them the total sales dollar amount for a couple different time ranges, these would be my columns (ie last day, last week, last month, last year, total ever). I would like to give these values for orders that are in different status' (ie complete, paid but not shipped, in progress). Something similar to this:

|OrderStatus|Today|LastWeek|LastMonth|
|Processed  |$10  |$100    |$34000   |
|PaidNotShip|$4   |$12     |$45      |

My question: What is the best/most efficient way to do this? I know that I could write separate SQL statements and union them together and bind the gridview to a sqldatasource:

(select amountForYesterday, amountForLastWeek from sales where orderStatus = processed)
UNION 
(select amountForYesterday, amountForLastWeek from sales where orderStatus = paidnotshipped) 

But that seems like a pain and very inefficient, since I would effectively be writing a separate query for each value.

I could also do this in the .cs page behind on load and programmatically populate the grid view row by row.

This GridView would only show information for the user's specific organization, so it would have to filter based on that as well.

I'm kind of at a loss as to how to do this without writing a massive query and continually hitting that query and database each time the page is viewed.

Any ideas?

I prefer using LINQ to work with data and/or GridViews (accessing the rows etc.). Have a look at a project I have on GitHub, which does exactly what I am mentioning here, as example. Note that this is just a sandbox I used previously for illustration purposes.

GitHub Repo

https://github.com/pauloosthuysen/int

Other useful info:

http://www.codeproject.com/Articles/33685/Simple-GridView-Binding-using-LINQ-to-SQL

The Sales etc. for LastWeek and LastMonth does not change very often. You could store that in a static Dictionary indexed by organization or summarize it in a separate table for faster access. This way you will not need to select the same huge amount of rows to get the same numbers over and over again. Unless special demands I would stick to the Dictionary solution because it is simple but a combination could also be a good solution

There is no direct way of doing it. However instead of hitting the DB to the sum of every columns, you can perform the stuff using you datatable which is used for binging to your grid.

All you need to do is use

Dim iSumSal As Integer

iSumSal = StudentTable.Compute("SUM(sal)", "")

similarly you can perform for other columns.

once this is done. then just add a new row to you data table with all the summed values in it.

And then you can bind it to your grid.

optional - you can put some text value in the first column of you new row as "Total:"

thanks

rahul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM