473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DataColumn.Expression with DateTime Datatype

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
Mar 8 '07 #1
11 21815
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.

Mar 8 '07 #2
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/lib...9(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.

Mar 8 '07 #3
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?
Mar 8 '07 #4
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
Mar 9 '07 #5
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-****@online.microsoft.comwrote in message
news:rF**************@TK2MSFTNGHUB02.phx.gbl...
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


Mar 13 '07 #6
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" <mf****************@nospam.nospamwrote in message
news:O9*************@TK2MSFTNGP04.phx.gbl...
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-****@online.microsoft.comwrote in message
news:rF**************@TK2MSFTNGHUB02.phx.gbl...
>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



Mar 13 '07 #7
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

Mar 13 '07 #8
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

Mar 13 '07 #9
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-****@online.microsoft.comwrote in message
news:Xe**************@TK2MSFTNGHUB02.phx.gbl...
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

Mar 13 '07 #10
Hi Matt,

Thank you for your prompt response.

How about my solution of handling the ColumnChanged event of the data
table? Is your problem solved now?

If the problem is still not solved or you have any question, please feel
free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!
Sincerely,
Linda Liu
Microsoft Online Community Support

Mar 14 '07 #11
The problem has been taken care of.... thanks for your help, it's certainly
appreciated.

"Linda Liu [MSFT]" <v-****@online.microsoft.comwrote in message
news:o3**************@TK2MSFTNGHUB02.phx.gbl...
Hi Matt,

Thank you for your prompt response.

How about my solution of handling the ColumnChanged event of the data
table? Is your problem solved now?

If the problem is still not solved or you have any question, please feel
free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!
Sincerely,
Linda Liu
Microsoft Online Community Support

Mar 15 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: J | last post by:
Is there anyway to format a calculated datacolumn? I would like the following column to display as money (formatted as $#,##0.00). Or how about simply displaying the column formatted as a number...
1
by: Dde | last post by:
Hi everybody, I have a problem with the "IN" operator in a datacolumn expression. I have a datatable on which the following expression works: mycode = '05' but the following does not work:...
1
by: Jason | last post by:
First off, I tried to find an ADO.NET forum, or even just a .NET framework forum but couldn't seem to locate one and I apologize in advance if I've posted in the wrong place. Because I'm using a...
0
by: vijayalakshmi.venkataraman | last post by:
Hello, I have a .NET client that accesses a Java webservice. Let me first say that the client stub I got generated from the wsdl was incomplete and had to make changes to it manually to make it...
3
by: Larry Bertolini | last post by:
For some reason, I recall having read that SQL Server 2005 would support a datatype that represented date, but not time. (This would be useful for storing things like birthday, where you usually...
6
by: rn5a | last post by:
I am inserting records in a MS-Access database table. The data type of one of the columns named *OrderDate* in the DB table is Date/Time. This is the SQL query I am using to insert the records in...
0
by: SMH | last post by:
Hi All, I am currently learning .Net 2, studying for 70-528. I've hit a bit of a brick wall with DataColumn.Expression. As I understand it, this can be used to (For example) concatenate two...
1
by: jelling | last post by:
Hi, Is it possible to use a CASE or IF ELSE statement in a datacolumn expression? Here's what I've tried: 'standardCodeColumn.Expression = "CASE StandardsBodyID WHEN 1 THEN 'JCAHO' ELSE...
1
by: Vajrala Narendra | last post by:
Hi am working with asp.net with backend Sql2000 i want to store NULL in datetime datatype field through coding. i wrote a update statement as Update table set Date1='NULL' where ........ but...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.