DataGridView将时间字段更改为日期吗? [英] DataGridView changing time field to date?

查看:77
本文介绍了DataGridView将时间字段更改为日期吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在加载.csv文件,并将其显示在datagrid中.但是,在.csv字段中,我的时间类似于00:30:00,但是何时在datagrid 12/30/1899 12:30 AM中显示.

这就是我的阅读方式:

Hi all,

I am loading a .csv file and just showing it in a datagrid. However, in the .csv field I have time like 00:30:00 but when is showed in the datagrid 12/30/1899 12:30 AM.

This is how I am reading it:

OleDbConnection ExclConn = new OleDbConnection();
                // HDR = YES get header; Extended Properties = text parsing a text file
                ExclConn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
                                            "Data Source=" + dir + ";" +
                                            "Extended Properties=\"text;HDR=Yes;FMT=Delimited\"";
                //Open the Excel File
                string Exceldata = "SELECT * FROM [" + FileName + "]";
                OleDbDataAdapter dtadpt = new OleDbDataAdapter(Exceldata, ExclConn);
                //Create table
                dtadpt.Fill(dt);


我希望能够仅保留该格式以将其保存到sql数据库中(我在那儿使用time(0)作为数据类型.

有任何建议吗?


I want to be able to just keep that format to save it into the sql database (which I am using there time(0) as the datatype.

Any suggestion?

推荐答案

贾尔蒙特,

它被读取为整数值,因此前导零无意义.

如果不在CSV和数据网格之间转换值,则无法知道现在是时间.

您需要以某种方式指定该单元格包含一个日期/时间值,如何执行仅取决于您在数据网格中显示CSV的准确程度.

问候,
弗兰克(Frank)
Hi jalmonte,

It is being read as an integer value, thus the leading zeros mean nothing.

If you don''t convert the value between the CSV and the datagrid, it has no way of knowing it is a time.

You need to specify in some fashion that that cell contains a date/time value, how you do that just depends on how exactly you are displaying the CSV in the datagrid.

Regards,
Frank


看看有关CellFormatting事件处理程序的MSDN页面. [
Take a look at the MSDN page for the CellFormatting event handler.[^].

The example on that page shows how to format dates/times. Just alter it to suit your input and desired output.


这篇关于DataGridView将时间字段更改为日期吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆