Solved

Add cells in a datagrid

Posted on 2009-07-10
17
240 Views
Last Modified: 2012-05-07
I have a datagrid in vb6.  I have columns 10 through 35 that should have numbers in them.  From the datagrid I want them to autoTotal into column 36.  Is there a way to do this.  Its kind of simalar to what Microsoft Excel does with the SUM function.
0
Comment
Question by:al4629740
  • 9
  • 8
17 Comments
 
LVL 9

Assisted Solution

by:wellhole
wellhole earned 500 total points
ID: 24826682
Add a column to your datasource, then loop through it and run a sum and put the result into the column you added.

table.Columns.Add("mysum")

for each row in table
  for i = 10 to 35
    table("mysum") += row(i)
  next
next
0
 

Author Comment

by:al4629740
ID: 24826730
Is that code for vb6?  The following line does not work

table("Total") += row(i)
0
 
LVL 9

Expert Comment

by:wellhole
ID: 24826733
oops...

row("mysum") += row(i)
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:al4629740
ID: 24826747
Still no go..
0
 
LVL 9

Expert Comment

by:wellhole
ID: 24826764
And your problem is.......?
0
 

Author Comment

by:al4629740
ID: 24826814
Meaning your formula does not work.  Will this work?

For Each Row In Table
  For i = 10 To 35
    Row("Total") = Row("Total") + Row(i)
  Next
Next
0
 
LVL 9

Expert Comment

by:wellhole
ID: 24826837
You forgot Table.Rows in your first line.
0
 

Author Comment

by:al4629740
ID: 24826865
Could you show me what your talking about.  I am very novice to VB
0
 
LVL 9

Expert Comment

by:wellhole
ID: 24826886
It would be very helpful if you provided the error messages instead of just saying "it doesn't work".
0
 

Author Comment

by:al4629740
ID: 24826919
Ok, originally you sent the code and it showed Expected:expression   Compile error on this line

table("mysum") += row(i)
0
 
LVL 9

Expert Comment

by:wellhole
ID: 24826929
Right. Now that you have the code below, what errors do you have now?

For Each Row In Table.Rows
  For i = 10 To 35
    Row("Total") = Row("Total") + Row(i)
  Next
Next
0
 

Author Comment

by:al4629740
ID: 24826944
Object required in the first line

For Each Row In Table.Rows
0
 
LVL 9

Expert Comment

by:wellhole
ID: 24826975
al, is your table variable called Table?
0
 

Accepted Solution

by:
al4629740 earned 0 total points
ID: 24827042
ha, good catch.  I changed it to the following:

For Each Row In DataGrid1.Row
  For i = 10 To 35
    Row("Total") = Row("Total") + Row(i)
  Next
Next

But I still get an error on the first line:

Compile error:
For Each may only iterate over a collection object or array

0
 
LVL 9

Expert Comment

by:wellhole
ID: 24827066
al, is your datagrid binding to a table or are you adding into the grid programmatically?
0
 

Author Comment

by:al4629740
ID: 24827094
Programmatically, as follows:

      rec.CursorType = adOpenDynamic
      rec.CursorLocation = adUseClient
      rec.LockType = adLockOptimistic
      rec.Open esql, conn, , , adCmdText



     
        Set DataGrid1.DataSource = rec
0
 

Author Comment

by:al4629740
ID: 24828554
raised point value
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question