带有DateTime数据类型的DataColumn.Expression [英] DataColumn.Expression with DateTime Datatype

查看:67
本文介绍了带有DateTime数据类型的DataColumn.Expression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些看起来应该很简单的事情,但

还没有找到解决方案。我正在尝试将数据列添加到数据表

,根据表格中的另一个日期时间列添加或减去天数。见下面的示例。我该怎么做呢?任何帮助都会非常值得赞赏。谢谢

Private Sub CreateDataTable()

dt =新数据表


Dim IntegerColumn As New DataColumn

IntegerColumn.DataType = GetType(整数)

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 =" Com putedDateColumn"

ComputedDateColumn.Expression =" DateColumn.AddDays(2)" ''这个

不起作用

ComputedDateColumn.Expression =" DateColumn + 2" ''这个

也不起作用

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

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

推荐答案

我做的不要以为你可以使用一个简单的Expression

列来做这个,因为没有可用的函数可以选择一个日期,并且

转换函数只会强制执行DateTime到一个字符串,并且

试图在具有有限的子串函数的

表达式中挑选一个通用格式的日期时间字符串将是一个挑战。

请参阅 http:// msdn2 .microsoft.com / zh-CN / lib ... xpression.aspx


如果您尝试在D中显示此数据ataGridView,然后一个

解决方案是使用未绑定列并处理

CellValueNeeded事件以按需提供此计算值。 (如果你想要更多关于这个想法的信息,请询问

。)

===================== < br $>
Clay Burch

Syncfusion,Inc。

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.


Hi Matt,

Transact-SQL提供了一些日期和时间函数,例如DateAdd,

DateDiff等等。遗憾的是,DataColumn的

表达式不支持这些函数,这意味着我们无法在
中使用DateAdd函数表达DataColumn。


我建议你使用select sql语句中的DateAdd函数来获取
检索DataTable。以下是一个示例。


SELECT DATEADD(day,2,DateColumn)FROM YourTable

有关DataAdd函数的更多信息,可以访问以下

链接。


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


希望这会有所帮助。

如果您有任何疑问,请随时告诉我。


此致,

Linda Liu

Microsoft在线社区支持


======== ==========================================

通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。


注意:MSDN托管新闻组支持服务是针对非紧急问题

如果社区或微软支持人员在1个工作日内做出初步回复是可以接受的。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========


此帖子按原样提供。没有保证,也没有授予任何权利。

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.


琳达,


所提供的解决方案非常适用于点。问题是在填充了

数据表之后,然后我将表单上的控件绑定到表格。

显然,我不想重新查询数据库每次更改日期是

。有关如何处理此事的任何建议吗?
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?


这篇关于带有DateTime数据类型的DataColumn.Expression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆