0

I'm calling a SQL select statement and displaying the results in a Data Grid. All is working fine EXCEPT that in SQL the order_date column displays as '2004-01-30 09:35:52.000' while the column in my DataGrid displays as '1/30/2004 9:35 AM'. The main problem is that I'm losing the 'seconds' (52 in this example).. I need the seconds value. As a secondary issue, I'd like to know how this works so I can format how the date/time displays in my DataGrid.

The full extent of the little C# program I'm testing with is below. Pretty simple.. and I don't think I'm explicitly defining the date/time format in any way..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Test_to_Get_Seconds_From_SQL
{
    public partial class Form1 : Form
    {
        SqlConnection m_cnSQLConnection;
        SqlDataAdapter m_daDataAdapter;
        DataTable m_dtOrders;
        SqlCommand m_comSQLCommand;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnShowOrders_Click(object sender, EventArgs e)
        {
            // Set Connection string
            string strSQLConnection;
            strSQLConnection = @"Initial Catalog=Sandbox;Data Source=WFDW3B79\SQLEXPRESS;Persist Security Info=True;Integrated Security=True;";

            // Open connection
            using (m_cnSQLConnection = new SqlConnection(strSQLConnection))
            {
                m_cnSQLConnection.Open();
                // Create new DataAdapter
                using (m_daDataAdapter = new SqlDataAdapter("SELECT * FROM Orders", m_cnSQLConnection))
                {
                    // Use DataAdapter to fill DataTable
                    m_dtOrders = new DataTable();
                    m_daDataAdapter.Fill(m_dtOrders);

                    // Render data onto the screen
                   dataGridView.DataSource = m_dtOrders;
                }
            }    
        }
    }
}

2 Answers2

1

You need to change the format of the Grid column to display the date in the required format, this is not a problem with your SQL.

How to format DateTime columns in DataGridView?

Community
  • 1
  • 1
ChrisBint
  • 12,773
  • 6
  • 40
  • 62
  • In the code above nothing is done with the DataGrid to declare columns prior to running the SELECT statement. The solution is to just write it better, but for my own education, the DataGrid above knows nothing about what the columns will hold until after I use the DataDapter to Fill the DataTable, then define the DataTable to be the DataGrid source. For instance, I could swap out the Orders table for some other table with no datetime columns and still run this code successfully. I assume either the Fill() or the Grid itself has some default setting for datetime display? Can I change that? – user2434008 Jun 18 '13 at 20:39
1

Supply a date format to your boundfield, for longtime: -

<asp:boundfield DataField="DateColumn" DataFormatString="{0:G}" HtmlEncode="false" />

More on date format strings

DGibbs
  • 14,316
  • 7
  • 44
  • 83