473,722 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataColumn.Expr ession 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.D ataType = GetType(Integer )
IntegerColumn.C olumnName = "IntegerCol umn"
dt.Columns.Add( IntegerColumn)

Dim ComputedInteger Column As New DataColumn
ComputedInteger Column.DataType = GetType(Integer )
ComputedInteger Column.ColumnNa me = "ComputedIntege rColumn"
ComputedInteger Column.Expressi on = "IntegerCol umn + 2"
dt.Columns.Add( ComputedInteger Column)

Dim DateColumn As New DataColumn
DateColumn.Data Type = GetType(System. DateTime)
DateColumn.Colu mnName = "DateColumn "
dt.Columns.Add( DateColumn)

Dim ComputedDateCol umn As New DataColumn
ComputedDateCol umn.DataType = GetType(System. DateTime)
ComputedDateCol umn.ColumnName = "ComputedDateCo lumn"
ComputedDateCol umn.Expression = "DateColumn.Add Days(2)" ' this
doesn't work
ComputedDateCol umn.Expression = "DateColumn + 2" ' this
doesn't work either
dt.Columns.Add( ComputedDateCol umn)

Dim r As DataRow = dt.NewRow
r.Item("Integer Column") = 5
r.Item("DateCol umn") = Date.Today

dt.Rows.Add(r)

End Sub
Mar 8 '07 #1
11 21909
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
'ComputedDateCo lumn' 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 ComputedDateCol umn
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.mic rosoft.comwrote in message
news:rF******** ******@TK2MSFTN GHUB02.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
'ComputedDateCo lumn' 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 ComputedDateCol umn.

"Matt F" <mf************ ****@nospam.nos pamwrote in message
news:O9******** *****@TK2MSFTNG P04.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 ComputedDateCol umn
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.mic rosoft.comwrote in message
news:rF******** ******@TK2MSFTN GHUB02.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
'ComputedDateC olumn' 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.
InitializeCompo nent()

' Add any initialization after the InitializeCompo nent() call.
AddHandler Me.DataSet11.Ta ble1.ColumnChan ged, AddressOf
Table1_ColumnCh anged
End Sub

Sub Table1_ColumnCh anged(ByVal sender As Object, ByVal e As
DataColumnChang eEventArgs)
If (e.Column.Colum nName = "DateColumn ") Then
e.Row("Computed DateColumn") =
Convert.ToDateT ime(e.ProposedV alue).AddDays(2 )
' to ensure the
DateTimePicker control bound to the ComputedDateCol umn to display the new
value immediately, call the corresponding BindingManagerB ase's Refresh
method
CType(BindingCo ntext(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.mic rosoft.comwrote in message
news:Xe******** ******@TK2MSFTN GHUB02.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

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

Similar topics

3
18724
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 with only 2 decimals (#,##0.00)? In the sample below, amount and taxrate are existing DataColumn's. DataColumn colTax = new DataColumn(); colTax.DataType = System.Type.GetType("System.String"); colTax.ColumnName = "tax";...
1
6469
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: mycode IN ('01','05') Do you have any clues ? Thanks,
1
2870
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 dataset in VB.NET I figured this was the next logical place to ask my question. I'm using the DataColumn.Expression property, and when I call the function with a '\' I get a crash.
0
1652
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 work to an extent. I generated a java client stub for the java web service and have been modifying my .NET client stub using the java client stub as a reference and things seemed to work fine until I got the following error. In my client...
3
14773
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 don't care about the time of day). But I've got SQL Server 2005 installed, and there's no such datatype to be found. Is this something that might be released in a Service Pack, or is it just not going to happen?
6
3390
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 the Access DB table: =============================== strSQL = "INSERT INTO Cart (CartID, ProductID, Quantity, Total, OrderDate) VALUES (CID, PID, Qty, TotalAmt, ODate)" oleDbCmd = New OleDbCommand(strSQL, oleDbConn)
0
4565
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 columns to make a derived column. When I run my code, I get this error: ------------
1
7979
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 'Non-JCAHO' END" 'standardCodeColumn.Expression = "CASE WHEN
1
1278
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 while updating it is giving error converting varchar to datetime how to store NULL in that field please respond Thanks in Advance
0
8739
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.