PC Review


Reply
Thread Tools Rate Thread

DataColumn.Expression with DateTime Datatype

 
 
Matt F
Guest
Posts: n/a
 
      8th Mar 2007
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


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


 
Reply With Quote
 
 
 
 
ClayB
Guest
Posts: n/a
 
      8th Mar 2007
I do not think you will be able to do this using a simple Expression
column as there are no available functions to pick apart a date, and
the Convert function will only coerce a DateTime to a string, and
trying to pick apart a genericly formatted datetime string in an
Expression with the limited Substring function will be a challenge.
See http://msdn2.microsoft.com/en-us/lib...xpression.aspx.

If you are trying to display this data in a DataGridView, then one
solution would be to use an unbound column and handle the
CellValueNeeded event to provide this computed value on demand. (Ask
if you want more ifnormation on this idea.)
=====================
Clay Burch
Syncfusion, Inc.

 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      8th Mar 2007
Hi Matt,

Transact-SQL provides some date and time functions, such as DateAdd,
DateDiff and so on. Unfortunately, these functions are not supported in the
expression of a DataColumn, which means we couldn't use DateAdd function in
the expression of a DataColumn.

I suggest you use the DateAdd function in the select sql statement to
retrieve the DataTable. The following is a sample.

SELECT DATEADD(day, 2, DateColumn) FROM YourTable

For more information on the DataAdd function, you may visit the following
link.

'DATEADD'
http://msdn2.microsoft.com/en-us/library/aa237919(SQL.80).aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
Matt F
Guest
Posts: n/a
 
      8th Mar 2007
Linda,

The solution provided works great up to a point. The problem is after the
datatable is populated, I then bind controls on a form to the table.
Obviously, I don't want to have to requery the DB every time a date is
changed. Any suggestions for how to deal with this?


 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      9th Mar 2007
Hi Matt,

Thank you for your feedback.

Do you bind two DateTimePicker controls on the form to the two datetime
columns in the datatable, respectively?

If so, I think a simple workaround is to subscribe the ValueChanged event
of the DateTimePicker that is bound to the 'DateColumn' column in the
datatable, and change the value of the DateTimePicker that is bound to the
'ComputedDateColumn' column.

You may call the AddDays method of DateTime to get the desired value for
the computed DateTimePicker.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support


 
Reply With Quote
 
Matt F
Guest
Posts: n/a
 
      13th Mar 2007
Sorry for the delay in response, I've had another issue that took my
attention away from this.

While the suggestion I beleive to be a good one, the problem is this is an
application where the controls are created dynamically at runtime and as
such, it's quite possible the date control bound to the ComputedDateColumn
will be created before the date control bound to the DateColumn --- as such,
it's not to reliably subscribe to the ValueChanged event since the control
may or may not exist yet.

"Linda Liu [MSFT]" <v-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Matt,
>
> Thank you for your feedback.
>
> Do you bind two DateTimePicker controls on the form to the two datetime
> columns in the datatable, respectively?
>
> If so, I think a simple workaround is to subscribe the ValueChanged event
> of the DateTimePicker that is bound to the 'DateColumn' column in the
> datatable, and change the value of the DateTimePicker that is bound to the
> 'ComputedDateColumn' column.
>
> You may call the AddDays method of DateTime to get the desired value for
> the computed DateTimePicker.
>
> Hope this helps.
> If you have any question, please feel free to let me know.
>
> Sincerely,
> Linda Liu
> Microsoft Online Community Support
>
>



 
Reply With Quote
 
Matt F
Guest
Posts: n/a
 
      13th Mar 2007
Another problem with working from the data layer instead of the data
layer --- I need the ability to do things like set the sort and filter
properties of my bindingsource based on values from the ComputedDateColumn.

"Matt F" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Sorry for the delay in response, I've had another issue that took my
> attention away from this.
>
> While the suggestion I beleive to be a good one, the problem is this is an
> application where the controls are created dynamically at runtime and as
> such, it's quite possible the date control bound to the ComputedDateColumn
> will be created before the date control bound to the DateColumn --- as
> such, it's not to reliably subscribe to the ValueChanged event since the
> control may or may not exist yet.
>
> "Linda Liu [MSFT]" <v-(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi Matt,
>>
>> Thank you for your feedback.
>>
>> Do you bind two DateTimePicker controls on the form to the two datetime
>> columns in the datatable, respectively?
>>
>> If so, I think a simple workaround is to subscribe the ValueChanged event
>> of the DateTimePicker that is bound to the 'DateColumn' column in the
>> datatable, and change the value of the DateTimePicker that is bound to
>> the
>> 'ComputedDateColumn' column.
>>
>> You may call the AddDays method of DateTime to get the desired value for
>> the computed DateTimePicker.
>>
>> Hope this helps.
>> If you have any question, please feel free to let me know.
>>
>> Sincerely,
>> Linda Liu
>> Microsoft Online Community Support
>>
>>

>
>



 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      13th Mar 2007
Hi Matt,

Thank you for your feedback.

I understand you scenario.

I think an alternative to handle the ValueChanged event of the
DateTimePicker control is to handle the ColumnChanged event of the
DataTable, instead. (Of course, we still need to use the Transact-SQL
function 'DateAdd' in the select command for the initial query.)

The following is a sample.

Public Class Form1

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
AddHandler Me.DataSet11.Table1.ColumnChanged, AddressOf
Table1_ColumnChanged
End Sub

Sub Table1_ColumnChanged(ByVal sender As Object, ByVal e As
DataColumnChangeEventArgs)
If (e.Column.ColumnName = "DateColumn") Then
e.Row("ComputedDateColumn") =
Convert.ToDateTime(e.ProposedValue).AddDays(2)
' to ensure the
DateTimePicker control bound to the ComputedDateColumn to display the new
value immediately, call the corresponding BindingManagerBase's Refresh
method
CType(BindingContext(DataSet11, "Table1"), CurrencyManager).Refresh()
End If
End Sub
End Class


Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      13th Mar 2007
Hi Matt,

To sort or filter data in a data source, we could use DataView or
BindingSource as the media data source.

That is, set the DataView's Table property to the DataTable, or set the
DataSource and the DataMember properties of the BindingSource to the
DataSet and the DataTable, respectively. And then bind the controls to the
DataView or BindingSource.

As for DataView, we could use its Sort and RowFilter properties to sort and
filter the data.

As for BindingSource, we could use its Sort and Filter properties to sort
and filter the data.

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

 
Reply With Quote
 
Matt F
Guest
Posts: n/a
 
      13th Mar 2007
Linda,

Thanks for the clarification --- I'm already using the sort and filter
properties of the datasource and all is working well --- this is why the
calculations must be peformed at the data level and not the control level.

Thanks

"Linda Liu [MSFT]" <v-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Matt,
>
> To sort or filter data in a data source, we could use DataView or
> BindingSource as the media data source.
>
> That is, set the DataView's Table property to the DataTable, or set the
> DataSource and the DataMember properties of the BindingSource to the
> DataSet and the DataTable, respectively. And then bind the controls to the
> DataView or BindingSource.
>
> As for DataView, we could use its Sort and RowFilter properties to sort
> and
> filter the data.
>
> As for BindingSource, we could use its Sort and Filter properties to sort
> and filter the data.
>
> Hope this helps.
> If you have any question, please feel free to let me know.
>
>
> Sincerely,
> Linda Liu
> Microsoft Online Community Support
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2.0 : DataColumn.DataType John A Grandy Microsoft ADO .NET 1 18th Feb 2006 08:05 AM
Changing a DataColumn's DataType David P. Donahue Microsoft C# .NET 0 29th Mar 2005 12:29 PM
Calculations with DateTime fields using DataColumn.Expression stri =?Utf-8?B?TWF0dA==?= Microsoft Dot NET Framework Forms 2 2nd Jan 2005 06:19 AM
DateTime of Now in a DataColumn expression Jon Fairchild Microsoft ADO .NET 6 23rd Apr 2004 08:05 PM
subtracting DateTime fields in a DataColumn expression Jon Fairchild Microsoft ADO .NET 1 22nd Apr 2004 12:15 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:10 PM.