datagridview中的日期值 [英] Date value in datagridview

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

问题描述

你好,
使用此方法时,我需要将datagridview中的日期值更改为(dd/MM/yyyy hh:mm AM/PM)而不是(MM/dd/yyyy hh:mm AM/PM),
[DataGridView1.Columns("The_Date").DefaultCellStyle.Format ="dd"/"MM"/"yyyy hh":"mm tt"]
只是格式更改而不是实际值,
我需要将datagridview导出到excel
我用了这个
[excel.Columns("B:B").numberformat ="dd/MM/yyyy hh:mm AM/PM"]

当电脑的日期是mm/dd/yyyy时,一切正常,
但是,当电脑的日期为dd/mm/yyyy时,该值在(1-12)之间的天数内无法在excel中正确显示,这意味着1/2/2012(2月1日)显示为2/1/2012 (1月2日),

所以我需要更改datagridview中的日期值,而不仅仅是格式,
有什么办法吗?

谢谢,

Hello,
I need to change the date value in datagridview to (dd/MM/yyyy hh:mm AM/PM) instead of (MM/dd/yyyy hh:mm AM/PM), when used this
[ DataGridView1.Columns("The_Date").DefaultCellStyle.Format = "dd''/''MM''/''yyyy hh'':''mm tt" ]
just the format changed not the actual value,
I need to export datagridview to excel
I used this one
[ excel.Columns("B:B").numberformat = "dd/MM/yyyy hh:mm AM/PM" ]

When the pc''s date is mm/dd/yyyy everything is ok,
But when the pc''s date is dd/mm/yyyy the value doesn''t view correctly in excel for the days between (1-12) means 1/2/2012 (1st Feb) appears as 2/1/2012 (2nd Jan),

So I need to change the date value in datagridview not just format,
Is there any way to do that?

Thanks,

推荐答案

使用设计师 [
Use Column.DefaultCellStyle.Format[^] property or set it in designer[^]

or
dataGridView1.Columns[0].DefaultCellStyle.Format = "MM'/'dd'/'yyyy";




您可以设置所需的格式:


or

You can set the format you want:

dataGridViewCellStyle.Format = "MM/dd/yyyy";
this.date.DefaultCellStyle = dataGridViewCellStyle;
// date being a System.Windows.Forms.DataGridViewTextBoxColumn



你可以这样..



you can do like this..

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    // If the column is the Artist column, check the
    // value.
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist")
    {
        if (e.Value != null)
        {
            // Check for the string "pink" in the cell.
            string stringValue = (string)e.Value;
            stringValue = stringValue.ToLower();
            if ((stringValue.IndexOf("pink") > -1))
            {
                e.CellStyle.BackColor = Color.Pink;
            }

        }
    }
    else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")
    {
        ShortFormDateFormat(e);
    }
}

//Even though the date internaly stores the year as YYYY, using formatting, the
//UI can have the format in YY.  
private static void ShortFormDateFormat(DataGridViewCellFormattingEventArgs formatting)
{
    if (formatting.Value != null)
    {
        try
        {
            System.Text.StringBuilder dateString = new System.Text.StringBuilder();
            DateTime theDate = DateTime.Parse(formatting.Value.ToString());

            dateString.Append(theDate.Month);
            dateString.Append("/");
            dateString.Append(theDate.Day);
            dateString.Append("/");
            dateString.Append(theDate.Year.ToString().Substring(2));
            formatting.Value = dateString.ToString();
            formatting.FormattingApplied = true;
        }
        catch (FormatException)
        {
            // Set to false in case there are other handlers interested trying to
            // format this DataGridViewCellFormattingEventArgs instance.
            formatting.FormattingApplied = false;
        }
    }
}



您能否通过
更多信息 [ ^ ]



would you pls go through this link for More Info[^]


由于DateFormat CurrentCulture 的影响,我认为在表单加载事件中可以设置CurrentCulture ,然后将CustomFormat 设置为以下栏目
Since the DateFormat is influenced by the CurrentCulture I think in the form load event the CurrentCulture can be set and then the CustomFormat may be set to the DataGridView Column as below
'Set in the Form.Load event
Thread.CurrentThread.CurrentCulture = 
        System.Globalization.CultureInfo.InvariantCulture
        
DataGridView1.Columns("The_Date").DefaultCellStyle.Format  = 
        "dd/MM/yyyy hh:mm tt"


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

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