Results 1 to 7 of 7

Thread: How to show am/pm for the time in the datagridview after insert the time into sql tab

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Posts
    121

    How to show am/pm for the time in the datagridview after insert the time into sql tab

    Hello,,,
    I am working on testing project, This project has label which show time with am\pm. But after adding the whole data to table, am\pm doesn't appear in the datagridview as shown in the picture below. So How to show am/pm for the time in the datagridview after insert the time into sql table?

    Here label which show time with am\pm.
    Code:
                label8.Text= string.Format("{0:hh:mm tt}", DateTime.Now);

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: How to show am/pm for the time in the datagridview after insert the time into sql

    Hi,

    The code below, shows dates with time and tt. Assuming you are populating the datagridview control via DataTable.

    Code:
    private void Form1_Load(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
    
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("Book", typeof(string));
        table.Columns.Add("Borrowed Date", typeof(DateTime));
        table.Columns.Add("Returned Date", typeof(DateTime));
    
        table.Rows.Add(1, "Book A", string.Format("{0:MM/dd/yyyy  HH:mm tt}", DateTime.Now.AddDays(-10)), string.Format("{0:hh:mm tt}", DateTime.Now));
        table.Rows.Add(2, "Book B", string.Format("{0:MM/dd/yyyy  HH:mm tt}", DateTime.Now.AddDays(-10)), string.Format("{0:hh:mm tt}", DateTime.Now));
        table.Rows.Add(3, "Book C", string.Format("{0:MM/dd/yyyy  HH:mm tt}", DateTime.Now.AddDays(-10)), string.Format("{0:hh:mm tt}", DateTime.Now));
    
        dataGridView1.DataSource = table;
    }
    -kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,230

    Re: How to show am/pm for the time in the datagridview after insert the time into sql

    Formatting is generally done at the column level in a DataGridView. Whatever format specifier you would usually use to format a single value, assign that to the DefaultCellStyle.Format property of the column. That format will then be used for each individual value in that column. This goes for numbers and dates/times.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Posts
    121

    Re: How to show am/pm for the time in the datagridview after insert the time into sql

    I already did that format but still not working

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Posts
    121

    Re: How to show am/pm for the time in the datagridview after insert the time into sql

    Quote Originally Posted by jmcilhinney View Post
    Formatting is generally done at the column level in a DataGridView. Whatever format specifier you would usually use to format a single value, assign that to the DefaultCellStyle.Format property of the column. That format will then be used for each individual value in that column. This goes for numbers and dates/times.
    I already did that format but still not working

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,230

    Re: How to show am/pm for the time in the datagridview after insert the time into sql

    Then you did it wrong. If you don't explain the details of what you actually did, we can't see what you did wrong. If you specify a valid date/time format string in the column and then populate that column with DateTime values, those values will be formatted as specified.

  7. #7
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: How to show am/pm for the time in the datagridview after insert the time into sql

    Quote Originally Posted by Max45 View Post
    I already did that format but still not working
    As jmc suggested, setting the DataGridView DefaultStyle.Format property is the ideal approach for formatting values.

    Code:
    private void Form1_Load(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
    
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("Book", typeof(string));
        table.Columns.Add("Borrowed Date", typeof(DateTime));
        table.Columns.Add("Returned Date", typeof(DateTime));
    
        table.Rows.Add(1, "Book A", DateTime.Now.AddDays(-10), DateTime.Now);
        table.Rows.Add(2, "Book B", DateTime.Now.AddDays(-10), DateTime.Now);
        table.Rows.Add(3, "Book C", DateTime.Now.AddDays(-10), DateTime.Now);
    
        dataGridView1.DataSource = table;
        dataGridView1.Columns[2].DefaultCellStyle.Format = "MM/dd/yyyy hh:mm tt";
        dataGridView1.Columns[2].Width = 150;
        dataGridView1.Columns[3].DefaultCellStyle.Format = "MM/dd/yyyy hh:mm tt";
        dataGridView1.Columns[3].Width = 150;
    }
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

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