MUKESH
MUKESH

Reputation: 69

How to add a 'Sum' column in a DataGrid?

I need 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?

Upvotes: 0

Views: 1929

Answers (1)

Emond
Emond

Reputation: 50672

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

Upvotes: 1

Related Questions