Calculated in datasets

Hi there

Probably an easy one but can I calculate in datasets, basically i need to caluclate the result of too coulmns and enteret them into another column on the dataset before updatting the changes back to the server

is this possible and has anyone any sample code to point me in the right direction


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.

Something on these lines should do it

        Dim Result As Integer
        For Each dr As DataRow In ds.Tables("MyTable").Rows
            Result = dr.Item("FirstFigure") * dr.Item("SecondFigure")
            dr.Item("Result") = Result

You may not want to do it for every row.  It may not be a multiplication.  You will need to substitute your own table name and column/field names.

But that's the basic idea.


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
davomanAuthor Commented:
ok this looks good i will try it out
ADO.Net support the concept of Expressions...   You can create an 3rd column in your DataTable and assign it an expression that will automagically perform the math on the other two columns.   The cool part is that it happens in real time, so if you change on of the dependent columns, the expression column will update also.

Here is a trivial example:

      Dim dc As DataColumn
      dc = New DataColumn("TotalStock")
      dc.DataType = GetType(Integer)
      dc.Expression = "UnitsInStock + UnitsOnOrder" ' two column types

I agree.  The only problem with that approach here might be the bit in the question that says "before updatting the changes back to the server".  It sounds like it's an existing column that is being recalculated and adding an expression column might make writing back more difficult.

Still, it is a choice and if davoman wants to go down that route, any such difficulties can be overcome.

davomanAuthor Commented:
Thanks that was just what i needed :)
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
Visual Basic.NET

From novice to tech pro — start learning today.