• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

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!
0
4eyesgirl
Asked:
4eyesgirl
  • 4
  • 3
  • 2
1 Solution
 
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.

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

Bob
0
 
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?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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.
0
 
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.

0
 
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.
0
 
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
0
 
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.
0
 
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now