簡體   English   中英

DataGridView上的日期格式不符合預期

[英]date on DataGridView not formatting as expected

我有一列使用在列樣式中設置的DateTime格式。

用戶輸入日期時,不會將其格式化為所設置的樣式。 事實上,它在該領域沒有做任何事情。 我在里面放了些亂七八糟的東西。

我在這里想念什么嗎? 它是否應該至少格式化日期或為錯誤的日期格式提供錯誤?

嘗試在datagridview上處理事件,獲取文本輸入並驗證是否為日期。 我使用了事件CellEndEdit ,但是可以根據需要選擇您的事件。

在此處輸入圖片說明

private void CheckCellValue(object sender, DataGridViewCellEventArgs e)
{
        //my column index on the date is 0; modify yours as needed!
    if (e.ColumnIndex == 0 && e.RowIndex > -1 && e.RowIndex != dataGridView1.NewRowIndex)
    {
        //check whether this can be parsed as a date.
        string enteredVal = dataGridView1.CurrentCell.Value.ToString();
        DateTime dt;
        if (DateTime.TryParse(enteredVal, out dt))
        {
            dataGridView1.CurrentCell.Value = dt.ToString("dd-MMM-yyyy");
        }
        else
        {
            MessageBox.Show("Doh, that's not a date: " + enteredVal);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM