如何格式化dataGridView中的数据

时间:2013-06-27 15:07:17

标签: c# .net datetime datagridview formatting

我在DataGridView申请中有C#/WinForms

我想在单元格中输入日期。

如果用户输入01/01/2012,则没关系,但如果输入01012012,则会出现例外情况。

我可以使用CellValidating事件验证输入。

尽管如此,如果用户输入01012012之类的日期,我想自动格式化,显然,我需要在CellValidated事件中执行此操作。

这是我的代码:

private void dataGridView_BadgeService_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateDebut" || dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateFin")
    {
        String date = Convert.ToString(e.FormattedValue).Trim();
        if (date.Length > 0)
        {
            try
            {
                DateTime _date;
                if (DateTime.TryParse(date, out _date) == false)
                {
                    if (DateTime.TryParseExact(date, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out _date) == false)
                    {
                        MessageBox.Show("Merci de saisir une date, ou laissez cette zone vierge", "Action-Informatique", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        e.Cancel = true;
                    }
                }
            }
            catch
            {
                MessageBox.Show("Merci de saisir une date, ou laissez cette zone vierge", "Action-Informatique", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                e.Cancel = true;
            }
        }
    }
}

private void dataGridView_BadgeService_CellValidated(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateDebut" || dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateFin")
    {
        String date = Convert.ToString(dataGridView_BadgeService.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).Trim();
        if (date.Length > 0)
        {
            try
            {
                DateTime _date;
                if (DateTime.TryParse(date, out _date) == true)
                {
                    dataGridView_BadgeService.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = _date.ToShortDateString();
                }
                else
                {
                    if (DateTime.TryParseExact(date, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out _date) == true)
                    {
                        dataGridView_BadgeService.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = _date.ToShortDateString();
                    }
                }
            }
            catch
            {
                MessageBox.Show("Merci de saisir une date, ou laissez cette zone vierge", "Action-Informatique", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}

我不知道为什么,但如果我输入01012012,则CellValidated事件不会触发。我有一个DataGridView Exception关于DateTime的错误格式。

如何自动格式化日期以避免此错误?

它说:“字符串不被识别为有效的DateTime”

非常感谢, Nixeus

1 个答案:

答案 0 :(得分:0)

我纠正了自己,我需要在CellValidating事件中使用CancelEdit():

       private void dataGridView_BadgeService_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        if (dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateDebut" || dataGridView_BadgeService.Columns[e.ColumnIndex].Name == "DateFin")
        {
            string date = Convert.ToString(e.FormattedValue).Trim();

            if (date.Length > 0)
            {
                try
                {
                    DateTime _date;
                    if (DateTime.TryParse(date, out _date) == false)
                    {

                        if (DateTime.TryParseExact(date, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out _date) == false)
                        {
                            MessageBox.Show("Merci de saisir une date, ou laissez cette zone vierge", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            e.Cancel = true;
                        }
                        else
                        {
                            dataGridView_BadgeService.CancelEdit();
                            e.Cancel = false;
                        }
                    }
                    else
                    {
                        dataGridView_BadgeService.CancelEdit();
                        e.Cancel = false;
                    }


                }
                catch
                {
                    MessageBox.Show("Merci de saisir une date, ou laissez cette zone vierge", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    e.Cancel = true;
                }
            }
        }