Expressions in a Datatable

G

Guest

Hello everyone

Here is what I am trying to do

I have a datatable with 7 columns (called Mon, Tue, Wed,...) representing the days of the week

I want to create a calculated column called totals that totals the 7 days of the week columns

I try to use the following code

Dim cTotal As DataColumn
cTotal = New DataColumn
With cTotal
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "First Totals"
.Expression = "Sun + Mon + Tue + Wed + Thu + Fri + Sat"
End With
dt.Columns.Add(cTotal)

But it does not work.

If I put in the following as the expression the column calculates fine

..Expression = "Sun + Mon"

Is it possible for me to add the values of 7 columns into 1 totals column?

Thanks

Corey
 
G

Guest

Update,

I now know why my code was not working

When I was doing my calcluation expression, I was not checking to see if a column had nulls in it.

I changed the expression to look like this:

.Expression = "IsNull(Sun,0) + IsNull(Mon,0) + IsNull(Tue,0) + IsNull(Wed,0) + IsNull(Thu,0) + IsNull(Fri,0) + IsNull(Sat,0)"

And now it works!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top