Binding DB2 (iSeries) DATE/TIME/TIMESTAMP columns to a WinForms DataGridView
I am trying to pull the contents of an AS/400 file back to a da开发者_StackOverflow中文版ta-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)
Try the following:
SELECT MYDATE,
MYTIME,
TIMESTAMP(CHAR(MYDATE,ISO)||'-'||CHAR(MYTIME,ISO))
FROM LIBNAME.FILENAME
WHERE <blah blah blah>
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.
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.
精彩评论