简体   繁体   中英

displaying table data on datagridview

I am using windows application and displaying table data from sqlite to datagriview but i cant understand why my date format changes during display.

sqlite date format DateTime 2012-02-20 16:42:10.000

datagridview date format 20/02/2012 16:42:10

my code

SQLiteConnection m_dbConnection;

m_dbConnection = new SQLiteConnection("Data source = F:/Explor/final test/WebMobility.db; Version=3;");

m_dbConnection.Open();

SQLiteCommand myCommand = new SQLiteCommand();
myCommand.Connection = m_dbConnection;


myCommand.CommandText = "select CompanyId,DateTime,description  from  VerbaliData";

DataTable data = new DataTable();
SQLiteDataAdapter myAdapter = new SQLiteDataAdapter(myCommand);

myAdapter.Fill(data);
dataGridView1.DataSource = data;
this.dataGridView1.Refresh();
if (dataGridView1.RowCount > 0)
{
    string value = "";
    DataGridViewRow dr = new DataGridViewRow();
    StreamWriter swOut = new StreamWriter("F:/Explor/final test/finaltest12.csv");

    //write header rows to csv
    for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
    {
        if (i > 0)
        {
            swOut.Write(",");
        }
        swOut.Write(dataGridView1.Columns[i].HeaderText);
    }

    swOut.WriteLine();

    //write DataGridView rows to csv
    for (int j = 0; j <= dataGridView1.Rows.Count - 1; j++)
    {
        if (j > 0)
        {
            swOut.WriteLine();
        }

        dr = dataGridView1.Rows[j];

        for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
        {
            if (i > 0)
            {
                swOut.Write(",");
            }

            value = dr.Cells[i].Value.ToString();
            //replace comma's with spaces
            value = value.Replace(',', ' ');
            //replace embedded newlines with spaces
            value = value.Replace(Environment.NewLine, " ");

            swOut.Write(value);
        }
    }
    swOut.Close();
}

m_dbConnection.Close();

There are 2 things in this, Datetime format that SqlLite is saving is called Universal Format and its accessible across all application. This helps us to save us from Localisation issues. sqlite date format DateTime 2012-02-20 16:42:10.000

DataGrid is showing this because its picking up your system format ie DD/MM/YYYY, if you need to show it in same format you will need to declare is explictily. eg Date.ToUniversalTime() datagridview date format 20/02/2012 16:42:10

Database always stores as yyy-MM-dd format. Date in your DatGrid shows the format based on your machine date format.

you can see this by below example. Query your database to get current date like below

select FORMAT(GETDATE(),'dd/MM/yyy');

the result would be 2014-03-12 14:50:45.450

you can change this by Format function.

select FORMAT(GETDATE(),'dd/MM/yyy');

Change your query like below would fix the issue.

select CompanyId, FORMAT(GETDATE(),'yyy-MM-dd hh:mm:ss'), description from  VerbaliData;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM