Click to See Complete Forum and Search --> : Set Column type in a datagrid
trekmp
March 30th, 2004, 09:22 AM
How can I set a column in a datagrid to be a DateTime type.
The reason being I store my date values in the database as doubles so the dates look like 37032 and I can convert them easy enough in my application to look like 21/05/2001, but the datagrid is bound to the data source so of course it used the field type from the database, which is Float.:(
gasperCasper
March 31st, 2004, 01:11 PM
Dont know if this is the best way!
1) Add a new column (custom col; say custDateTime as datetime) to your data source (assuming your datasource is a dataset. you can open the XML of your dataset and add the new col via designer. it is simple.)
2) Fill your dataset (are you using dataadapter?)
3) Before binding to datagrid, loop thru all the rows in the dataset and assign your custom column (custDateTime) the values that you obtained by converting doubles.
i.e.
for each dr in dataset.tables(0).rows
dr.custDateTime = cdate(dr.DoubeValVar)
next
4) now bind the dataset to the datagrid. But, bind the new cust col to your date time col in the datagrid.
Does that work?
klegesdal
October 28th, 2004, 10:35 PM
I having some trouble with displaying datetime as mm/dd/yyyy hh:mm:ss in the datagrid.
My project is using an access 2000 database to fill the grid via a dataset generated from an oledbadapter. I am attempting to sort the datagrid using a dataview.sort method....
this will work when I have the xml Date element set as a datetime type. however i am only seeing the date portion in the datagrid.
if I change the type to String, I see the date and the time in the datagrid. This would be ok, however, when I sort the column as type string the sorting does not work properly.
Finally, what I'm moving toward is adding a cmbo box to above the grid so the user can select a "30 day", "60 day"...etc view. I have got this to work when I use the datetime type...but of course I can't see the time displayed with the date.
seems like a simple format issue....?? well simple for someone anyone....
Thanks for any help
kenny
klegesdal
October 29th, 2004, 08:36 PM
I guess the one solution is to have one column for the date and one for the time. And do a 2 column sort...
gasperCasper
October 31st, 2004, 09:59 AM
I guess there is a better solution than the one you mentioned in your latest reply.
Datarid has a property called TableStyles. Each TableStyles has in turn GridColumnStyles (simple hierarchy; datagrid --> Table --> Columns). Each GridColumnStyles has a FORMAT property.
For the datetimeColumn that you want to display as DATE + TIME, set the format property as "MM/dd/yyyy hh:mm:ss".
For example, if the name of the column in your datagrid is dateTimeTempCol, then set it's format property as:
dateTimeTempCol.Format = "MM/dd/yyyy hh:mm:ss" '(or any other standard formatting string "MM/dd/yyyy", etc).
Does this help?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.