Sorting ASP.NET GridView in a calculated field
4 answers
Put the original returned data in DATASET or DATATABLE. Add a new column to DATATABLE for the calculated field. Pass this data by doing the necessary calculations and placing the result in the specified calculated field.
Create a new view based on the datatable and sort the view by the calculated field. Bind the grid to your data view.
Dim DT as DataTable
DT = GetDataTableFromDataBaseMethod()
DT.Columns.Add(New DataColumn("CalculatedColumnName"))
For each row as DataRow in DT.Rows
row("CalculatedColumnName") = PerformCalculations(row)
Next
Dim view as New DataView
view.DataTable =dt
View.Sort = "CalculatedColumnName DESC"
datagrid1.Datasource = view
datagrid1.Databind
Or, if possible, do the calculations in the SQL statement, re:
SELECT Col1, Col2, Col3, Col1+Col2+Col3 AS LineTotal FROM Table;
+7
source to share