Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Totals in a Dataset? .NET

Status
Not open for further replies.

twifosp

Programmer
Jul 25, 2003
186
US
If I'm creating a dataset
Code:
DataSet ds = new DataSet();
da.Fill(ds);
dgViewer.DataSource = ds;
dgViewer.DataBind();

Is there a way to:

1. sum of all the columns for totals.

2. insert that back into the dataset so it renders inside the table

Thanks
 
Hi,
A DataSet object contains a collection of DataTable objects. A DataTable object is representing a database table with rows and columns which are the most important things of a Datatable object.
There is no Sum() function to do totals directly on a given column but you could write it in few statements by iterating all rows of the DataTable object.
Let be dt a DataTable object with a column named Amount and a type of data Integer:
int sum =0;
foreach (DataRow dr in dt.Rows)
{
sum+=(int)dr["Amount"];
}
If the Amount column has a type of System.string but there are in fact numbers , then:
int sum =0;
foreach (DataRow dr in dt.Rows)
{
sum+=System.Convert.ToInt32(dr["Amount"].ToString());
}
-obislavu-





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top