在 C#/Winform 中,如果用户输入,我可以将字符串解析为日期:dd/mm/yyyy
DateTime.Parse(date).ToString();
我希望能够在没有斜杠的情况下进行解析(例如在 datagridview 或 DateTimePicker 中)。
01022012应该解析为01/02/2012
任何人都知道如何解析它DateTime.Parse?
这是我的代码:
    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;
                    DateTime.TryParseExact(date, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out _date);
                    date = _date.ToShortDateString();
                    dataGridView_BadgeService.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = date;
                }
                catch
                {
                   MessageBox.Show("Merci de saisir une date, ou laissez cette zone vierge", "Action-Informatique", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                   e.Cancel = true;
                }
            }
        }
    }
这是异常消息:

它说:“System.FormatException:字符串不被识别为日期时间验证”