Calculating values in a datagrid
-
Hi everyone Im using VB.Net 2003. I have a field in a datagrid "Total", how do i calculate the total:sigh: and Display it in a label
I would suggest enumerating all rows of the datasource (datatable) and summing up each value from the target column. It would look something like this: (assuming there is a DataTable already created called DT
Dim Total As Double = 0#
For Each R As DataRow In DT.Rows
Total += R(1) 'This line simply adds the value from Column 1 of the current row.
Next
Me.label1.Text = Total.ToString() 'Set the labels text property to display the total.If you have a strongly typed DataTable, you may have access to the Properies of each DataRow to use instead of getting the column data using the index of in the row. For example R.Price instead of R(1). Hope that does the trick. "Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem." ( President Ronald Reagan) -- modified at 21:50 Monday 28th August, 2006