Solved

adding total row to datagrid in window forms .net

Posted on 2004-08-08
3
243 Views
Last Modified: 2012-06-27
How to add the row below datagrid last row which display the total of the column above as in excel
so it behave like excel sheet.
0
Comment
Question by:gary_1nz
3 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 11749821
web forms: http://www.dotnetspider.com/Technology/KB/ShowSample.aspx?SampleId=133 http://www.devasp.net/net/search/res/r19286.html

the easiest way is probably to just use aggregate columns (DataColumn.Expression) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataDataColumnClassExpressionTopic.asp also see http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcreatingexpressioncolumns.asp and http://msdn.microsoft.com/library/en-us/dndive/html/data05312002.asp

an alternative would be to create the new row with code similar to ...

dim dt as DataTable = DataSet.Tables("Whatever")
dim dr as DataRow
dim Totals as DataRow = dt.NewRow()
dim i as integer
for i = 0 to Totals.Columns.Count - 1
      Totals[i] = 0
end for
foreach dr in dt
      for i = 0 to dr.Columns.Count - 1
            Totals[i] += dr[i];
      end for
end for
dt.Rows.Add(Totals)

this code assumes all fields are numeric, for real life you would probably want to check the datatype of the column.

Cheers,

Greg
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now