将 DB2 (iSeries) DATE/TIME/TIMESTAMP 列绑定到 WinForms DataGridView
我试图将 AS/400 文件的内容拉回到数据绑定的 .NET WinForms DataGridView (VS 2010)。查询本身没有问题,我可以使用网格的 DataSource 属性绑定所有内容。数据返回没有任何问题。
我遇到的问题是所有日期/时间字段都以字符串文字形式返回,导致无法排序。此外,它返回时使用我们默认的 AS/400 格式(yyyy-dd-mm 和 hh.mm.ss)。我希望它们作为实际的日期/时间字段返回,以便可以对它们进行排序并且我可以控制输出格式(我更喜欢 MM/dd/yyyy 和 hh:mm:ss AMPM)。我尝试使用 TIMESTAMP 格式将这两个字段合并为一个,但这只是给了我一个组合字符串。
如果可能的话,我希望避免进行任何字段处理,并且我希望保持表数据绑定以便于编码,尽管如果我必须关闭列自动生成,我会这样做。我还想避免执行任何中间 LINQ 查询,因为我失去了开箱即用的按列排序的能力(而且我在网上看到的将其添加回来的示例都是漫长而痛苦的)。
有人可以建议什么吗?谢谢!
编辑:代码示例。查询值的 SQL(为了保护无辜而更改名称)是:
SELECT MYDATE, MYTIME, TIMESTAMP(MYDATE, MYTIME)
FROM LIBNAME.FILENAME
WHERE <blah blah blah>
SQL 查询通过 OleDbDataAdapter.Fill 命令写入 DataTable。
Dim myTable as New DataTable
Using adapter As New OleDbDataAdapter
adapter.SelectCommand = New OleDbCommand(<sql statement>, <AS/400 Connection>)
adapter.Fill(myTable)
Return myTable
End Using
DataTable 被填充到 DataGridView 中:
grid.DataSource = FunctionCallToGrabTheDataAbove
非常简单(如果可能的话尽量保持这种方式)
I am trying to pull the contents of an AS/400 file back to a data-bound .NET WinForms DataGridView (VS 2010). The query itself is no problem, and I am able to bind everything using the DataSource property of the grid. Data comes back with no issues.
The problem I am having is that all date/time fields come back as string literals, making it impossible to sort. Moreover, it comes back using our default AS/400 formats (yyyy-dd-mm and hh.mm.ss). I would like them to come back as actual Date/Time fields so that they can be sorted and I can control the output format (I would prefer MM/dd/yyyy and hh:mm:ss AMPM). I tried combining the two fields into one using the TIMESTAMP format, but that just gave me a combined string.
I would like to avoid doing any field massaging if at all possible, and I would like to keep the table data-bound for ease of coding, although if I have to turn off column auto-generation I will do so. I would also like to avoid doing any intermediary LINQ queries, as I lose the ability to sort by columns out of the box (and the examples I have seen online for adding this back in are all long and painful).
Can anybody suggest anything? Thanks!
EDIT: Code example. SQL to query the values (with names changed to protect the innocent) is:
SELECT MYDATE, MYTIME, TIMESTAMP(MYDATE, MYTIME)
FROM LIBNAME.FILENAME
WHERE <blah blah blah>
SQL query gets written to a DataTable via an OleDbDataAdapter.Fill command.
Dim myTable as New DataTable
Using adapter As New OleDbDataAdapter
adapter.SelectCommand = New OleDbCommand(<sql statement>, <AS/400 Connection>)
adapter.Fill(myTable)
Return myTable
End Using
DataTable gets stuffed into a DataGridView:
grid.DataSource = FunctionCallToGrabTheDataAbove
Pretty straightforward (and trying to keep it that way if possible)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请尝试以下操作:
Try the following:
问题是,AS/400 日期时间字段基本上是字符串文字。至少,当您检查 AS/400 上的物理文件时,它们是这样的。只要它们采用通常的时间戳顺序,即 YYYY.MM.DD.hh.mm.ss,它们就可以排序并且可以比较(彼此)。
如果这不能满足您的需求,您将不得不自己按摩这些区域或执行其他中间步骤。
The thing is, AS/400 datetime fields basically are string literals. At least, that is what they look like when you inspect the physical files on the AS/400. As long as they are in the usual timestamp order, i.e. YYYY.MM.DD.hh.mm.ss, they are sortable and comparable (with each other).
If this doesn't serve your needs, you will have to massage the fields or do other intermediate steps yourself.
我认为您必须引入 DataGridView 可以解释为日期而不是字符串的列。如果无法使用 DB2 SQL 来完成此操作,则可以在数据表中添加新的日期列。
I think you're going to have to introduce a column the DataGridView can interpret as a date instead of strings. If you can't do it with DB2 SQL, you can add a new date column in the datatable.