add calculated datacolumn to datagridview

Richnl

Well-known member
Joined
Mar 20, 2007
Messages
93
Programming Experience
Beginner
I have used the code below to add multiple calculated
columns, but how do, or better, can it be more simplyfied
, because it seems a bit redundant.

Only the last created column matters!

I need an expression that creates a total of the prices bound to
one primary key ID when I navigate threw the gridview!

This is not something I can use I think, because I need an expression based column?
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim [/COLOR][/SIZE][SIZE=2]sumObject [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE]
[SIZE=2]sumObject = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DBDataSet.Table1.Compute([/SIZE][SIZE=2][COLOR=#a31515]"Sum(Amount)"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]"YesNo = Yes"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]MsgBox(sumObject.ToString)[/SIZE]

VB.NET:
Private Sub AddColumn()   
[SIZE=2][/SIZE] 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cols [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataColumnCollection = _[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DBDataSet.Tables([/SIZE][SIZE=2][COLOR=#a31515]"Table1"[/COLOR][/SIZE][SIZE=2]).Columns[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myCol [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataColumn = _[/SIZE]
[SIZE=2]cols.Add([/SIZE][SIZE=2][COLOR=#a31515]"BedrgOpn"[/COLOR][/SIZE][SIZE=2], System.Type.GetType([/SIZE][SIZE=2][COLOR=#a31515]"System.Decimal"[/COLOR][/SIZE][SIZE=2]), _[/SIZE]
[SIZE=2][COLOR=#a31515]"IIF(YesNo = 1 , 0, Amount)"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]cols.Add([/SIZE][SIZE=2][COLOR=#a31515]"TotAmountOpn"[/COLOR][/SIZE][SIZE=2], System.Type.GetType([/SIZE][SIZE=2][COLOR=#a31515]"System.Decimal"[/COLOR][/SIZE][SIZE=2]), _[/SIZE]
[SIZE=2][COLOR=#a31515]"Sum(AmountOpn)"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[COLOR=green]'To the parenttable[/COLOR]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cols2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataColumnCollection = _[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DBDataSet.Tables([/SIZE][SIZE=2][COLOR=#a31515]"Table0"[/COLOR][/SIZE][SIZE=2]).Columns[/SIZE]
[SIZE=2]cols2.Add([/SIZE][SIZE=2][COLOR=#a31515]"AmountOpn"[/COLOR][/SIZE][SIZE=2], System.Type.GetType([/SIZE][SIZE=2][COLOR=#a31515]"System.Decimal"[/COLOR][/SIZE][SIZE=2]), _[/SIZE]
[SIZE=2][COLOR=#a31515]"Sum(child(FK_Table1_Table0).AmountOpn)"[/COLOR][/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2]cols.Add([/SIZE][SIZE=2][COLOR=#a31515]"FinalColumn"[/COLOR][/SIZE][SIZE=2], System.Type.GetType([/SIZE][SIZE=2][COLOR=#a31515]"System.Decimal"[/COLOR][/SIZE][SIZE=2]), _[/SIZE]
[SIZE=2][COLOR=#a31515]"Parent(FK_Table1_Table0).AmountOpn"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#008000]'for testing[/COLOR][/SIZE]
[SIZE=2]MsgBox(cols(cols.Count - 1).ToString)[/SIZE]
 
[SIZE=2]myCol.ReadOnly = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]myCol.Unique = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
End Sub

thanks, richard
 
Last edited:
I cant see, from that code, what youre trying to do?

Aggregate columns can only be computed from child rows, but it seems you know this..
 
In this case there are two tables related threw one column(custumerID)
Primarykey table has name/adress,etc info
The second table stores amounts that are to be paid to the custumer
It also has a column(YESNO), it stores 1/0 depending if an amount has been paid out.

The colums above insures I get the total amount (not yet paid out) per customer.

My actual question was, If It where possible to make less columns to get to the same endpoint (one column with the totals per customer)

Maybe a syntax with the individual expressions combined? that's all
 
Last edited:
I expect you would have an expression like:


SUM(IIF(child(FK_Table1_Table0).YesNo=1, 0, Amount))
 
That said... there isnt any guarantee that Expresion can support nested syntax like this...
In which case you made the next best option. You can hide the column from view if you set "AutoGenerateColumns" to false, on the grid
 
Back
Top