Search code examples
c#sql-serverdatagridviewdatetime2

showing full date & time in datagridview


I am having an application that shows date and time in datagridview after exporting data from database to dataset.

Details : I have a column called Date that contains date and time in datetime2 format and when showing date, seconds don't show up.

con = new SqlConnection();
con.ConnectionString = my_connection_string;
con.Open();
adap = new SqlDataAdapter("SELECT Date,Object,ASDU,IOA,Alarm from Alarms_List",my_connection_string);
ds = new System.Data.DataSet();
adap.Fill(ds, "Alarms_List");
dataGridView1.DataSource = ds.Tables[0];

Example : Date table contains 15/07/2016 10:03:13 It's shown as : 15/07/2016 10:03

I have also tried CAST(Date AS DATETIME2) in the select statement but none of them works


Solution

  • You must set the "Format", sample like this :

    dataGridView1.Columns[0].DefaultCellStyle.Format = "dd.MM.yyyy HH:mm:ss";