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!
4eyesgirlAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr33nlant3rnCommented:
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.

Bob LearnedCommented:
Are you using table styles with the DataGrid?

Bob
4eyesgirlAuthor Commented:
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?
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

gr33nlant3rnCommented:
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.
4eyesgirlAuthor Commented:
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.

gr33nlant3rnCommented:
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.
4eyesgirlAuthor Commented:
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
gr33nlant3rnCommented:
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.
Bob LearnedCommented:
I am talking about something like this:

    DataGridTableStyle style = new DataGridTableStyle();
    style.MappingName = this.dataSet1.Tables[0].TableName;

    DataGridTextBoxColumn column = new DataGridTextBoxColumn();
    column.Format = "dd MMM yyyy HH:mm:ss";
    column.HeaderText = "Date/Time";
    column.MappingName = "PostedDate";

    style.GridColumnStyles.Add(column);
   
    this.dataGrid1.TableStyles.Clear();
    this.dataGrid1.TableStyles.Add(style);

Bob

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.