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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrids 2

Status
Not open for further replies.
Jun 28, 2002
34
0
0
GB
Can someone please help. I am wanting to know is there a quick and easy method to totalling columns in a datagrid

many thanks
 
Paul

What is your grids datasource eg DataReader or a Datatable?


WTrueman
...if it works dont mess with it
 
Before I start I am using a Windows Form, not ASP, I think the code is probably similiar but I am less familiar with ASP.

I am using SQLDataAdapter and a Dataset to fill a datagrid. The grid is made up of 5 columns and numerous rows. I would like to total column 5 and display the result in a Text box.

Is this possible, Thanking you in advance
 
Paul

If you have a Dataset, then it contains one or more Datatables. You could replace the reference to dt (my datatable) with ds.tables(0)

Here's some code that would do the trick.

Code:
Dim mytotal As Double = 0
Dim foundrows() As DataRow = dt.Select("") 'Select all Rows
For Each drow As DataRow In foundrows
    mytotal += CDbl(drow.Item("kgm"))
Next


WTrueman
...if it works dont mess with it
 
Thanks for all your help on this matter unfortunately I am having a problem with the For each drow line, There appears to be a problem with drow as datarow.

I tried replacing the dt as you suggested with my ds.table
Heres what I tried

Dim myTotal as Double = 0
Dim FoundRows() as DataRow = ds.Tables("Orders").Select("Select * from Orders")
For each drow as DataRow in Foundrows
myTotal +=...Not sure how to reference the column I want to sum
next

I really need all the help I can get
 
Paul

Firstly you need the empty string in your select command.
The datatable select simply acts as a filter for your table, and you dont want to apply a filter. Check to see if you have any rows selected by checking foundrows.length

You can reference the drow.item() by either the field name, or its column position

drow.item("field-name") or drow.item(0)




WTrueman
...if it works dont mess with it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top