Ahmed Aekbj
Ahmed Aekbj

Reputation: 91

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

Upvotes: 0

Views: 2324

Answers (2)

Abhishek Tewari
Abhishek Tewari

Reputation: 425

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");

Upvotes: 1

user6522773
user6522773

Reputation:

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

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

Upvotes: 4

Related Questions