How To Display Time in hh:mm tt using DataGridView RRS feed

  • Question

  • Good afternoon,

    I'm trying to get the time formatted in hh:mm tt format. For example 02:30 PM. I have a database called SubJobs, a datagridvew called datagridvew1 and a column in SubJobs called Time which is a Time(7) datatype.

    This is the query I tried:

    string query2 = "select chName, MassType, Date, Time, Stipend from SubList where chID= '" + Person.churchID.ToString() + "'; ";

    This is the code I tried to format the column in DataGridView:

    dataGridView1.Columns[3].DefaultCellStyle.Format = "hh:mm tt";

    When I execute the code I get the following message:

    Any help would be greatly appreciated!



    • Edited by Steven8579 Friday, June 21, 2019 5:36 PM
    Friday, June 21, 2019 5:34 PM

All replies

  • Hello,

    Here is a mock up w/o a database. If the column coming from the database table is not a date time then that would be the issue.

    var dt = new DataTable();
    dt.Columns.Add(new DataColumn() {ColumnName = "SomeDate", DataType = typeof(DateTime)});
    dataGridView1.DataSource = dt;
    dataGridView1.Columns[0].DefaultCellStyle.Format = "hh:mm tt";

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Friday, June 21, 2019 6:34 PM