简体   繁体   English

在datagridview上显示表数据

[英]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. 我正在使用Windows应用程序,并显示从sqlite到datagriview的表数据,但是我不明白为什么我的日期格式在显示期间会改变。

sqlite date format DateTime 2012-02-20 16:42:10.000 sqlite日期格式DateTime 2012-02-20 16:42:10.000

datagridview date format 20/02/2012 16:42:10 datagridview日期格式20/02/2012 16:42:1020/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. 这有两件事,SqlLite保存的Datetime格式称为通用格式,并且可以在所有应用程序中访问。 This helps us to save us from Localisation issues. 这可以帮助我们避免本地化问题。 sqlite date format DateTime 2012-02-20 16:42:10.000 sqlite日期格式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. DataGrid之所以显示此信息,是因为它选择了您的系统格式(即DD / MM / YYYY),如果您需要以相同的格式显示它,则需要明确声明。 eg Date.ToUniversalTime() datagridview date format 20/02/2012 16:42:10 例如Date.ToUniversalTime()datagridview日期格式20/02/2012 16:42:10

Database always stores as yyy-MM-dd format. 数据库始终以yyy-MM-dd格式存储。 Date in your DatGrid shows the format based on your machine date format. DatGrid中的日期显示基于计算机日期格式的格式。

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 结果将是2014-03-12 14:50:45.450

you can change this by Format function. 您可以通过Format功能更改此设置。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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