Datacolumn Expression Question

Using Visual Basic 2005.
Have a typed dataset. Want to add a calculated column, that is the sum of the Owing column for each client. The client field is called Ref
Any ideas how I would do this?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If I'm understanding you correctly, I'm not sure you can do this.  At least easily.  

If you add an Expression Column, that is restricted to calculations on the data in the row concerned.  So, if each row has columns for Quantity and Price you can add an Expression Column for Total - Quantity * Price.  Then, on a row which shows a quantity of 2 and a price of 4 the Total column will show 8: if 3 and 6, 18; and so on.

Or you can use DataTable.Compute to get (for example) the sum of amounts in a particular column in all - or some filtered selection - of the rows.  So, that way, if each row contained a Client and a detailed Amount owing, you could use that to find the total owing for a particular client.

But it looks, from your question, as if you are trying to combine the two: that is, get a sum by totalling a particular column in a number of rows, but put that sum in a single row, or repeat it in a number of rows.  If that is really your objective you will, I think, have to make your additional column unbound, and write code (using DataTable.Compute) to calculate a sum for each client and then put the sum relating to each client in each row for that client.

jdhackettAuthor Commented:
Thanks. But what would the formula be for this datatable.compute?
I do want it repeated in each row. So if I had two clients, A and B, I'd like column 3 to be the sum of Owing. So it should look like
A   1   6
A   2   6
A   3   6
B   8   17
B   9   17

6 (1+2+3) being the total amount that A owes, 17 (8+9) being the total amount that B owes
I just can't figure out what the formula should be

Something like

   Dim TotalForClient As Double = myTable.Compute("SUM(Owing)", "Client = 'A'")

The first argument is the calculation, the second argument is the filter.  The datatype of TotalForClient should be that of the Owing column.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jdhackettAuthor Commented:
I want to put that in a report, so the datatable needs to have the calculated field.
So is the best way to loop through the datatable, running that calculation for each row, setting the unbound field to the TotalForClient calculation?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.