Link to home
Start Free TrialLog in
Avatar of 4eyesgirl
4eyesgirl

asked on

DataGrid display only Date but not Time

Hi expert,

I have a field in Access 2000 (called Start Date Time) which has the general date format (e.g. 4/04/2006 10:30:02 PM)

When I use the DataGrid control, it only display the Date (in this case 4/04/2006) but drop the Time (10:30:02 PM)
Codes : dataGrid1.SetDataBinding(dataSet1, "Elections");

But if I use the same dataSet1 to dump out the field value, it displays 4/04/2006 10:30:02 PM correctly.
Codes: DataTable dataTable = dataSet1.Tables[0];

      foreach (DataRow dataRow in dataTable.Rows)
      {
            listView1.Items.Add(dataRow["Election Start Date/Time"].ToString());
      }

Is there anyway I can make the DataGrid displays the date/time values in the correct format?

Thanks millions and hope to hear from you!
Avatar of gr33nlant3rn
gr33nlant3rn

From a high level, If you are not using autoGenerateColumns then configure the column's format string in the aspx page.  If you are using autogenerate, then use the onItemDataBound and cast that column of the datatable to a date and call ToString() and set the value of the cell needed.

Hope that helps.

Avatar of Bob Learned
Are you using table styles with the DataGrid?

Bob
Avatar of 4eyesgirl

ASKER

I drag and drop the DataGrid icon from the tool box.  I guess that what you means, Bob?

I don't know what is autoGenerateColumns ?  Can you explain a little bit in details?
I was asking if after you drag and drop the datagrid, do you set up the columns manually, or do you just databind and let the grid's column headers and columns get determined by the data in the datatable.  If you are setting up the columns manually, you can set properties on the columns to format the data for the cells in that column.  However if you just let the grid handle the columns on databind, you can "help" the grid by setting up the onItemDataBound event and cast the data in the datarow's column to a date, and then call the To(Your Format Preferred)DateString() method.
Can you tell me what is the column that I can set the columns format manually?  

OR you talk about DataBound event, can you give me an example with source code?  Sorry, I am not too familiar with DataGrid at all.

Sure here is a vb.net example   (The rest of the sample is available at http://www.dotnetjunkies.com/HowTo/CD157079-5F60-4031-8BFB-9F8C7478D586.dcik)

Protected Sub SearchResultsGrid_OnItemDataBound(ByVal sender As System.Object, ByVal e As DataGridItemEventArgs) Handles SearchResultGrid.ItemDataBound

   If SearchType.SelectedItem.Value.ToString() = "Numeric" Then
      If e.Item.Cells(0).Text = mySearchString Then
         e.Item.BackColor = System.Drawing.Color.Red
         e.Item.Font.Bold = True
      End If
   End If

End Sub


In the onItemDataBound event check the e.Item.Cells(x).Value and cast it to a date and call .ToLongDateString()

I hope that helps.
I am actually looking for C# example, but I am guessing this could help.  I will have to try to translate what you give me to C# and see it would works.

I will let you know
If you do a google search for OnItemDataBound and DataGrid there are lots of examples.  I develop in c# also, but the code is quite similar.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial