?
Solved

DataGrid display only Date but not Time

Posted on 2006-04-16
9
Medium Priority
?
322 Views
Last Modified: 2008-03-17
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
Comment
Question by:4eyesgirl
  • 4
  • 3
  • 2
9 Comments
 
LVL 1

Expert Comment

by:gr33nlant3rn
ID: 16466850
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16490089
Are you using table styles with the DataGrid?

Bob
0
 

Author Comment

by:4eyesgirl
ID: 16493948
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Expert Comment

by:gr33nlant3rn
ID: 16494006
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
 

Author Comment

by:4eyesgirl
ID: 16494086
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
 
LVL 1

Expert Comment

by:gr33nlant3rn
ID: 16494160
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
 

Author Comment

by:4eyesgirl
ID: 16494396
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
 
LVL 1

Expert Comment

by:gr33nlant3rn
ID: 16494419
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 16524433
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question