[英]How formatting a data in a dataGridView
我在C#/WinForms
應用程序中有一個DataGridView
。
我想在單元格中輸入一個日期。
如果用戶輸入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
事件。 我有一個有關DateTime
格式錯誤的DataGridView Exception
。
如何自動設置日期格式以避免此錯誤?
它說:“該字符串未被識別為有效的DateTime”
非常感謝Nixeus
我更正了自己,我需要在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;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.