0

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

Shrikant Mavlankar
  • 1,145
  • 1
  • 8
  • 17
Ahmed Aekbj
  • 91
  • 1
  • 12

2 Answers2

3

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

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

use :

select CONVERT(varchar(19), dateColumnName, 120) from tableName;

update:

You may also use the FORMAT function from SQL Server 2012 onwards as below:

select FORMAT(dateColumnName, date-format) from tableName;

ex

select FORMAT(GETDATE(), "dd/MM/yyyy hh:mm:ss");
Abhishek Tewari
  • 387
  • 3
  • 12