How to add a 'Sum' column in a DataGrid?
I nee开发者_如何学JAVAd to add some columns in the DataGrid view at run time.
I have nearly 12 columns in the DataGrid and I need to add 5 columns fully from top to bottom and show the sum result at last column.
How can I do that?
Assuming you are using a DataSet (ds) to fill the DataGrid:
Dim sumCol As DataColumn
sumCol = New DataColumn("Total", GetType(Double))
sumCol.Expression = "ColumnA + ColumnB + ColumnC + ColumnD" ' replace by the actual column names'
ds.Tables("TableName").Columns.Add(sumCol)
EDIT
This code calculates the sum for each row and adds a column.
If you want to calculate the sum of a column you will need to iterate over the rows and display the result yourself. You should not add that result as a row because you can only have a single type of row in a DataTable. A DataGrid is not a spreadsheet.
There are third party grids that allow you to add such features
精彩评论