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

Add values in one column of a datagrid 1

Status
Not open for further replies.

ponerse

Programmer
Feb 11, 2003
72
US
Hi, I have a datagrid with 3 columns and 6 rows. I would like to add all six rows of one column together. Here's what I have so far:

For x = 1 To DataGrid1.ApproxCount

total = total + DataGrid1.Columns(2).Text
Next x

This code however will only add the first row in the column 6 times. Can anyone tell me how I would go about advancing to the next row to add all the rows together?

Thanks!

Be an idiot:
 

In your xample you would need to use:
For x = 1 To DataGrid1.ApproxCount
DataGrid1.Row = x
total = total + DataGrid1.Columns(2).Text
Next x

But, this will not always work depending if the current row is with-in the first set of visible rows or not.

Use the following instead:
[blue]
Code:
Dim rsClone As ADODB.Recordset
Dim fld As ADODB.Field
Set rsClone = Adodc1.Recordset.Clone
[green]
Code:
'(replace Adodc1 with the name of the Adodc that the grid is bound to, or replace Adodc1.Recordset with the name of the recordset object variable that the grid id bound to)
[blue]
Code:
Set fld = rsClone.Fields(DataGrid1.Columns(2).DataField)
Do Until rsClone.EOF
    If Not IsNull(fld.Value) Then total = total + fld.Value
    rsClone.MoveNext
Loop
Set rsClone = Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top