Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataColumn.Expression in with DateTime

Status
Not open for further replies.

SavantMan

Programmer
Apr 17, 2002
165
US
I'm trying to do something that seems like it should be pretty simple, but haven't found a solution. I am trying to add a datacolumn to a datatable that adds or subtracts a number of days based on another datetime column in the table. See sample below. How do I do this? Any help would be greatly appreciated. Thanks

Code:
   Private Sub CreateDataTable()
      dt = New DataTable

      Dim IntegerColumn As New DataColumn
      IntegerColumn.DataType = GetType(Integer)
      IntegerColumn.ColumnName = "IntegerColumn"
      dt.Columns.Add(IntegerColumn)

      Dim ComputedIntegerColumn As New DataColumn
      ComputedIntegerColumn.DataType = GetType(Integer)
      ComputedIntegerColumn.ColumnName = "ComputedIntegerColumn"
      ComputedIntegerColumn.Expression = "IntegerColumn + 2"
      dt.Columns.Add(ComputedIntegerColumn)

      Dim DateColumn As New DataColumn
      DateColumn.DataType = GetType(System.DateTime)
      DateColumn.ColumnName = "DateColumn"
      dt.Columns.Add(DateColumn)

      Dim ComputedDateColumn As New DataColumn
      ComputedDateColumn.DataType = GetType(System.DateTime)
      ComputedDateColumn.ColumnName = "ComputedDateColumn"
      ComputedDateColumn.Expression = "DateColumn.AddDays(2)"  ' this doesn't work
      ComputedDateColumn.Expression = "DateColumn + 2"         ' this doesn't work either
      dt.Columns.Add(ComputedDateColumn)

      Dim r As DataRow = dt.NewRow
      r.Item("IntegerColumn") = 5
      r.Item("DateColumn") = Date.Today

      dt.Rows.Add(r)

   End Sub
 
try this:

Code:
Dim ComputedDateColumn As New DataColumn
      ComputedDateColumn.DataType = GetType(System.DateTime)
      ComputedDateColumn.ColumnName = "ComputedDateColumn"
      ComputedDateColumn.Expression = DateAdd("d",2,cdate("DateColumn"))       
      dt.Columns.Add(ComputedDateColumn)

I hope this works.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Ron,

The expression property is a string so :

Code:
ComputedDateColumn.Expression = DateAdd("d",2,cdate("DateColumn"))

must actually be:
Code:
ComputedDateColumn.Expression = "DateAdd(DateInterval.Day, 2, DateColumn)"

The problem is when this is run an error of: "The expression contains undefined function call DateAdd()." is returned.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top