Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying Data with Summary rows

Status
Not open for further replies.

henna

Technical User
Dec 18, 2002
23
0
0
NZ
Hi,
I have a database table which I want to display in the following format. A summary header with running totals.

10 August 1975
Description TotalExpense
aasdasd $1.50
asdasd $2.50
total $4.00

11 August 1976
Description TotalExpense
aasdasd $1.99
asdasd $2.50
total $4.00

Is there a way to display this information using datalist or repeaters or SQL queries.
 
Hi,

There's a couple ways I've done this in the past. Depending on what you're doing using "WITH ROLLUP" might be the easiest solution. Somehting like:

Code:
SELECT Description,Sum(Expense) AS TotalExpense WITH ROLLUP;

Another way is to use a datadapter to fill a dataTable and then loop through the dataTable and detect the change in the value (Decscription in this case). When you see a change, insert a new row into the datatable and insert your running total into it. Then you can bind the now modified datatable to a datagrid.
 
I should mention, I think WITH ROLLUP might only be available in SQL Server. Depending on what DBMS you're using, there might be a similar statment you can execute.
 
Hi,

Thanks for your reply. I am using SQL Server and also have tried the Rollup method.

My main problem is how to get the summary header information. eg the Date Header. I have also tried the DataReader loop but havnt been able to get the results.

I would really appreciate if you could send me source code sample for the DataTable loop and how you display the Header summary.

Once again, thanks for your quick response.

 
Hmm, yeah I guess I kinda ignored the whole part about the date headers huh?

One thing I can think of probably isn't the most efficient solution, but I think it will work. You can place a label and datagrid in a template column of another datagrid. You can bind the "outside" datagrid to a query that would return only the dates that you are interested in.

Then in the outside datagrid's ItemDataBound event, you can use the date that was returned to bind the "inside" datagrid to a query that will return the sums and descriptions for products associated with that date.

The downside is that you're going to hit the database once for a list of dates, and then again for each date (to fill the inside datagrid). An alternative would be to get the whoe dataset at once, and then use the DataTable.Select() method to select only the rows from the datatable during each ItemDataBound event (You'd have to declare the datatable at the page level I think).

I'm sorry I don't have any code here (no VS at home :( ), but I'll try to put something here that will get the idea across hopefully.

Code:
Dim decSumAmount as Decimal
Dim LastDate as Date
Dim i as Integer
Dim sqlQuery as String = "SELECT Date,Description,SUM(Amount) FROM aTable GROUP BY Date,Description"
Dim MyDataAdapter as new sqlDataAdapter(sqlQuery,SqlConnection1)


sqlconnection1.Open
MyDataAdapter.Fill(MyDataTable)
SqlConnection1.Close

For i = 0 to MyDataTable.Rows.Count - 1
  If myDataTable.Cells("Date") <> LastDate Then
    'this is a new row
     Dim MyDataRow as new DataRow
     MyDataRow = MyDataTable.NewRow  '(maybe MyDataTable.Rows.NewRow ? )
     MyDataRow.Cells("Amount") = decSumAmount
     MyDataTable.Rows.InsertAt(MyDataRow,i)
  Else 'add to the running total
     decSumAmount =+ MyDataTable.Cells(1)
     LastDate = MyDataTable.Cells("Date")
  End If
Next

MyDataGrid.DataSource = MyDataTable
MyDataGrid.DataBind

Or something like that. P-) I hope that isn't too full of errors. lol

FD
 
Great - it works now.
Thanks a lot for your help.
 
Another example can be found at the following URL and uses the Repeater control:


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top