导出datagrid值时的日期时间转换 [英] Date time conversion while exporting datagrid value

查看:78
本文介绍了导出datagrid值时的日期时间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本文件中导出我的datagrid值。我的系统日期格式是MM / dd / yyyy。在此代码中,Cell1和Cell2具有日期时间值。在屏幕上,它显示为dd / MM / yyyy格式,但在导出时,我的系统格式转换为MM / dd / yyyy。



请建议或提供处理此转换案例的方法,其中系统日期时间格式为MM / dd / yyyy。



============ ================================

  void  Export_Grid()
{
TextWriter sw = new StreamWriter( @ F:\\Attendance2013 \\Export.txt);
int rowcount = dataGridView1.Rows.Count;
for int i = 0 ; i < rowcount - 1 ; i ++)
{

sw.WriteLine(dataGridView1.Rows [i] .Cells [ 0 ]。Value.ToString()+ |
+ dataGridView1.Rows [i] .Cells [ 1 ] .Value.ToString()+ |
+ dataGridView1.Rows [i]。单元格[ 2 ]。Value.ToString()+
+ dataGridView1.Rows [i] .Cells [ 3 ]。Value.ToString()+ |
+ dataGridView1.Ro ws [i] .Cells [ 4 ]。Value.ToString()+ |
+ dataGridView1.Rows [i] .Cells [ 5 ]。Value.ToString()+ );

}
sw.Close();
MessageBox.Show( 数据导出成功);

}

解决方案

在DateTime值上使用默认的ToString实现时,它的格式是根据当前的系统设置。因此,由于您的PC设置为MM / dd / yyyy,因此日期格式为生成的字符串。为了避免这种情况,请使用带有ToString的格式strign:

 dataGridView1.Rows [i] .Cells [n] .Value.ToString(  dd / MM / yyyy





您可能还想考虑使用StringBuilder和File.WriteAllText而不是流和字符串连接 - 它可能会更快更有效。


您可以使用像这样...



 String.Format({0:MM / dd / yyyy},GridView1.Rows [i]。细胞[1]。价值)


I am exporting my datagrid value in text file. And my system date format is "MM/dd/yyyy". Here in this code, Cell1 and Cell2 has date time value. At the screen, it's visible as "dd/MM/yyyy" format but while exporting it converts in my system format as "MM/dd/yyyy".

Please suggest or provide way to handle this conversion case, where system date time format is MM/dd/yyyy.

============================================

void Export_Grid()
        {
            TextWriter sw = new StreamWriter(@"F:\\Attendance2013\\Export.txt");
            int rowcount = dataGridView1.Rows.Count;
            for (int i = 0; i < rowcount - 1; i++)
            {

                sw.WriteLine(dataGridView1.Rows[i].Cells[0].Value.ToString() + "|"
                + dataGridView1.Rows[i].Cells[1].Value.ToString() + "|"
                + dataGridView1.Rows[i].Cells[2].Value.ToString() + " "
                + dataGridView1.Rows[i].Cells[3].Value.ToString() + "|"
                + dataGridView1.Rows[i].Cells[4].Value.ToString() + "|"
                + dataGridView1.Rows[i].Cells[5].Value.ToString() + "");

            }
            sw.Close();
            MessageBox.Show("Data Exported Sucessfully");

        }

解决方案

When you use the default ToString implementation on a DateTime value, it is formatted according to the current system settings. So since your PC is set to MM/dd/yyyy, that is the format of the date as a string that is generated. To avoid that, use a format strign with the ToString:

dataGridView1.Rows[i].Cells[n].Value.ToString("dd/MM/yyyy")



You might also want to consider using a StringBuilder and File.WriteAllText instead of a stream and string concatenation - it'll probably be a lot quicker and more efficient.


You can use like this...

String.Format("{0:MM/dd/yyyy}", GridView1.Rows[i].Cells[1].Value)


这篇关于导出datagrid值时的日期时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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