DateTime in Datagrid

Hi there, 

i fill a datatable with a stored procedure and one of the fileds is a datetime datatype. But when the datatable is linked with the datagrid only the date (01-02-2005) is displayed. I want the time also in the datagrid. It is in the database and even in de field of the datagrid. 
I made a test in the mouseup event of the datagrid that shos a messagebox with the value transformed to ToLongTimeString. i get a messagebox with the time that is in the database. So the information is in the datagrid but how can i show the date and time in one field of the datagrid

( da = dataadapter,   dgLog = datagrid    dt = datatable )

 Dim da As New SqlClient.SqlDataAdapter(Select_log)
 Dim dt As New DataTable() 
 
 da.Fill(dt)
 dgLog.DataSource = dt

this is the code i used to fill my datagrid, the dataadapter is connected with a stored procedure though the "select_log" sqlcommand.
 
so please let me know how i can show the date and time together....


Answer this question

DateTime in Datagrid

  • Ayo Soul

    Hi

    Unfortunately, This InvariantInfo will not work because it is no datatype it is only a propery formated value. If you want only the time you can either store two values in the database date and time and than it will work with DateTimeFormatInfo.

    If you cant do so you have to write your own style component where you can define that.

    Regards,
    BonsaiVBNET

  • andrew M

    when i use this format (System.Globalization.DateTimeFormatInfo.InvariantInfo.ShortTimePattern) then i get an error:

    Column requires a valid datatype

  • Jendk

    Hallo Hallo,

    Yes there is another way to change the datatype of the datacolumn.

    Before you use the DataAdapter.Fill method to load your data in the dataset execute the DataAdapter.FillSchema method like this. Afterwards you change the dataype of the columns like you want. Just use the existing Schema and change whatever you want. I had the same problem with the datetime like you and the following code solve the problem.

    Try
                Me.sqlReportingAdapter.FillSchema(ds, SchemaType.Mapped, "ApplicationReport")
                ds.Tables("ApplicationReport").Columns(3).DataType = System.Type.GetType("System.Globalization.DateTimeFormatInfo")
                ds.Tables("ApplicationReport").Columns(4).DataType = System.Type.GetType("System.Globalization.DateTimeFormatInfo")
                Me.sqlReportingAdapter.Fill(ds, "ApplicationReport")
            Catch ex As Exception
                MessageBox.Show(ex.Message.ToString)
            End Try

    Cheers
    BonsaiVBNET

  • RoccoSmit

    You'll defininatly need to define TableStyle if you want to control the look and layout of your datagrid without modifing the grid itself.

    However, you might want to choose how to display the date (general system date/time formatting may not be most appropriate).  Here is a link to the DateTimeFormatInfo class:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemglobalizationdatetimeformatinfoclasstopic.asp

    This way you can format the date and time any way you choose.

  • Jamey Maxwell

    is there not another way to set the format to the "G" whitout creating a tablestyle 
    when i create a tablestyle all of my properties of my datagrid don't work anymore or i get a lot of errors... (properties like sorting, autocolumnresize, etc...) 

    i only want to display the date and time instead of only displaying the date... it must be possible to do this in a simple way and that i can hold to the properties that I allready have in my datagrid. 

    isn't it possible to set the format of one single column in a datagrid instead of making a tablestyle for the entire table...

    please let it be....

  • Jcastro

    Thanks a lot !!! 
    it works great !!! :-)

  • RussellF

    is there also a way that only the time is displayed 
  • Xavier Navarro

    You should be able to just be more specific with the data type.  Something like:

    "System.Globalization.DateTimeFormatInfo.InvariantInfo.ShortTimePattern"

    Or whichever format you choose.

  • Roy M Drenker

    Apply a DataGridTAbleStyle to the grid.  The column in question should have a DataGridTextBoxColumn with a Format of "G".
  • DateTime in Datagrid