Results 1 to 11 of 11

Thread: short time format in datagrid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124

    short time format in datagrid

    I have a dataset with 2 'SHORT TIME' format, but these 2 fields will show as a 'DATE' format in DataGrid. How to solve this problem? Anyone out there?

  2. #2
    Lively Member
    Join Date
    Jul 2003
    Location
    Kuala Lumpur (Malaysia)
    Posts
    92

    Hmmm...

    Hi,

    Are you loading the datagrid with data from the database ?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124

    Re: Hmmm...

    Originally posted by lianwk
    Hi,

    Are you loading the datagrid with data from the database ?
    YES, correct!

    datagrid.datasource = dataset

    and dataset has 2 'short time' fields, but display 'DATE' format in datagrid.

  4. #4
    Lively Member
    Join Date
    Jul 2003
    Location
    Kuala Lumpur (Malaysia)
    Posts
    92

    Two methods.....

    First method

    Direct format the fields from the query level.

    Second Methods.

    If you have added a table style to your datagrid (so individual column styles have been generated), then you can use code such as this to set the Format property of the particular column style.


    [VB.NET]

    Dim dgtbc as DataGridTextBoxColumn

    dgtbc = CType(dataGrid1.TableStyles(0).GridColumnStyles(3), DataGridTextBoxColumn)

    If Not dgtbc is Nothing Then
    dgtbc.Format = "g" ' or "u" or whatever format you want to see
    End If

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124

    Re: Two methods.....

    Originally posted by lianwk
    First method

    Direct format the fields from the query level.

    Second Methods.

    If you have added a table style to your datagrid (so individual column styles have been generated), then you can use code such as this to set the Format property of the particular column style.


    [VB.NET]

    Dim dgtbc as DataGridTextBoxColumn

    dgtbc = CType(dataGrid1.TableStyles(0).GridColumnStyles(3), DataGridTextBoxColumn)

    If Not dgtbc is Nothing Then
    dgtbc.Format = "g" ' or "u" or whatever format you want to see
    End If
    I don't have add a table style, and there is an error shown that "out of range", May i know what does GridColumnStyles(3) means?

    I'm just using a simple datagrid with setting the datasource to a dataset.

  6. #6
    Lively Member
    Join Date
    Jul 2003
    Location
    Kuala Lumpur (Malaysia)
    Posts
    92

    Sorry... lack of informations.

    The "3" in the code mean the column 4. and is adjustable.

    Anyway if want some source code referecne, you can goto:
    http://www.syncfusion.com/FAQ/WinFor...c44c.asp#q974q

    There got lots of solution for datagrid and the sample code in C# and VB.NET

    I recommended you use the first method because when you change the column count then you have to change the code also.
    You can use CAST or CONVERT in Microsoft SQL database.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124

    Re: Sorry... lack of informations.

    Originally posted by lianwk
    The "3" in the code mean the column 4. and is adjustable.

    Anyway if want some source code referecne, you can goto:
    http://www.syncfusion.com/FAQ/WinFor...c44c.asp#q974q

    There got lots of solution for datagrid and the sample code in C# and VB.NET

    I recommended you use the first method because when you change the column count then you have to change the code also.
    You can use CAST or CONVERT in Microsoft SQL database.
    I have a field is Date/Time format, example data = 8/21/2003 2:02:00. I want only retrieve the Time format which will display only 2:02:00. How to do that???

    I thought i can use Time(datetimefield1) instead of using only 'datetimefield1' in SQL statement.

    So...as you mentioned using the first method, can you please explain more clearly?

  8. #8
    Lively Member
    Join Date
    Jul 2003
    Location
    Kuala Lumpur (Malaysia)
    Posts
    92

    Wink Hmmm...

    Ok...

    Since you don't mention about what database you are using. So i assume you are using Microsoft SQL Server 2000.

    Assume the field named "myDateTime"
    Assume the table named "MyTable"

    the query is like below:

    SELECT CONVERT(VARCHAR(15), myDateTime, 108) FROM myTable

    where the VARCHAR(15) is the output you need after convertion.

    for other convertion please refer to the Microsoft SQL Server ebook which comes together with the installation CD. (part of the installation items)

    As i tested over here, it works. If you need futher assistance .... post... post ... and post.

  9. #9
    Member
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    62
    When creating a database you can choose to only have the time.

    Or you can edit the datagrid.
    Not sure on how to edit the datagrid without using a datetimepickler but with it.

    Dim test As String
    DateTimePickler1.Value = datagrid.item(row,column)
    test = DateTimePickler1.Value.ToShortTimeString
    MsgBox(test)

    You can have datagrid.item(row,column) instead of test
    Think that should give you only the time in the datagrid
    //Martin Andersson

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124

    Re: Hmmm...

    Originally posted by lianwk
    Ok...

    Since you don't mention about what database you are using. So i assume you are using Microsoft SQL Server 2000.

    Assume the field named "myDateTime"
    Assume the table named "MyTable"

    the query is like below:

    SELECT CONVERT(VARCHAR(15), myDateTime, 108) FROM myTable

    where the VARCHAR(15) is the output you need after convertion.

    for other convertion please refer to the Microsoft SQL Server ebook which comes together with the installation CD. (part of the installation items)

    As i tested over here, it works. If you need futher assistance .... post... post ... and post.
    OK...sorry, my database is Microsoft access database...can it be the same command?

  11. #11
    Lively Member
    Join Date
    Jul 2003
    Location
    Kuala Lumpur (Malaysia)
    Posts
    92

    Lightbulb Hmmm...

    Hi,

    Then study the query below:

    SELECT FORMAT(myColumn, "hh:mm:ss") FROM myTable;

    where
    myColumn = the datetime column
    myTable = table in the access database.

    * This is only example, for the correct format. Please refer to the Microsoft Access Format Setting... Enjoy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width