簡體   English   中英

WinForms DataGridView-在寫入DataSource之前編輯用戶輸入

[英]WinForms DataGridView - Edit user input before writing to DataSource

我正在創建一個用於記錄員工時間的應用程序。 所有用戶的所有時間數據都將存儲在UTC的SQL數據庫中。 前端將是一個Windows Forms應用程序,該應用程序顯示時間並接受用戶本地時區的輸入。 WinForms應用程序具有一個DataGridView控件,用戶可以在該DataGridView顯示和輸入時間。

DataGridView綁定到TimeSegment對象List TimeSegment是我創建的結構,基本上是這樣的:

struct TimeSegment 
{

    private DateTime start, end;

    public TimeSegment(DateTime start, DateTime end){
        Start = start;
        End = end;
    }

    public DateTime Start 
    {
        get { return start; } 
        set {
            if (value.Kind != DateTimeKind.Utc)
                throw new ArgumentException("Only UTC dates are allowed.");
            start = value;
        }
    }

    //There is an End property which is just like the Start 
    //property, but for the 'end' field.
}

因此,在DataGridView ,將有一列開始時間和一列結束時間,每一行代表用戶執行的某些事件或任務。

我知道如何通過使用DataGridView.CellFormatting事件將內部UTC時間轉換為本地時區以進行顯示。 我遇到的問題是在將更新發送到DataGridViewDataSource之前,在本地時間接受用戶輸入並轉換為UTC。

我看過其他幾篇文章,它們顯示了如何更改DataGridViewCellParsingCellValidating事件中單元格中的文本,但是我需要更改文本被解析為的實際對象,該對象將被發送到DataSource

有沒有一種方法可以覆蓋單元格的解析行為並指定應將哪個對象發送到DataSource?

您可以嘗試如下操作:

private void mainGrid_CellValidated(object sender, DataGridViewCellEventArgs e)
{
    //Convert time
    (mainGrid.Rows[e.RowIndex].DataBoundItem as TimeSegment).Start = convertedValue;
}

暫無
暫無

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

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