Solved

c# datatable calculated column

Posted on 2014-10-22
3
326 Views
Last Modified: 2014-10-23
Should be a simple question really...

I have a datatable in a c# app that has these columns:
GroupID
MemberID
MemberBalance
MemberWeight (need to calculate this) = MemberBalance / SUM(MemberBalance) Group by GroupID

Data looks like this:
data.png
0
Comment
Question by:timberadmin
  • 2
3 Comments
 
LVL 11

Expert Comment

by:louisfr
ID: 40397490
Create another table with GroupID and SumMemberBalance columns.
Create two child-parent relations (one in each direction) between the two tables.
Set the second table's MemberBalance Expression property to Sum(Parent.MemberBalance)
You can then set the Expression of the MemberWeight of your table to MemberBalance/Parent.SumMemberBalance
0
 

Author Comment

by:timberadmin
ID: 40397873
Thanks Louis. This works. Is there a way to do this without having to create additional tables?
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40399226
Not with a computed column, because there is no "group by" available. The other way is to do the work yourself, which means adding code to compute the column value each time you add, change or delete a row (e.g. using the RowChanged and RowDeleting events).

There was an error in my previous comment. You only need one relation, with the auxilliary table as parent, and the Expression would be "Sum(Child.MemberBalance)"
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

832 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