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(d ataSet1, "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(dataRo w["Electio n 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!
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(d
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(dataRo
}
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!
Are you using table styles with the DataGrid?
Bob
Bob
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 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.
ASKER
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.
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_OnItemDa taBound(By Val sender As System.Object, ByVal e As DataGridItemEventArgs) Handles SearchResultGrid.ItemDataB ound
If SearchType.SelectedItem.Va lue.ToStri ng() = "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.
Protected Sub SearchResultsGrid_OnItemDa
If SearchType.SelectedItem.Va
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.
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hope that helps.