2010-06-16 71 views
0

我正在使用SQL Server 2005和Visual Studio 2008,C#。如何操作綁定到數據源的DataGridView上的格式?

在數據源(SQL Server數據表)我使用的DateTime格式爲mm/dd/yyyy的,然而,在一個形式概述(DataGridView中)的用戶希望看到一個完全其他格式, (yyww,d)這是在字符串格式的年份,星期數和星期數。 我已經創建了一個值之間的轉換算法(日期到星期幾),但我可以用yyww,d(字符串)而不是mm/dd/yyyy(DateTime)填充受影響的單元格嗎?

這是從來就被檢測出來,但沒有成功 (和註釋,it's在最後一行的問題變得很明顯,作爲單元格的值不需額外接受運行時 - 它仍然想成爲一個日期時間 ...)

private void DataGridViewQueryFindRequests_CellFormatting(
    object sender, DataGridViewCellFormattingEventArgs e) 
    { 
    string weekAndDay = ""; 

    DataGridViewCell cell = 
     DataGridViewQueryFindRequests.Rows[e.RowIndex].Cells[e.ColumnIndex]; 


    if (cell.ColumnIndex == 13 && cell.Value == null) 
     mEmptyRow = true; 

    if ((cell.ColumnIndex == 14 || cell.ColumnIndex == 15) && !mEmptyRow) 
    { 
     weekAndDay = ClassWeeksAndDates.dateToWeekNumber(Convert.ToDateTime(cell.Value)); 

     cell.ValueType = typeof(string); 

     cell.Value = weekAndDay; 
    } 
    } 
+0

嗨,就是這個問題,從這個有什麼不同? http://stackoverflow.com/questions/3051192/is-it-possible-to-manipulate-the-format-on-a-datagridview-that-is-bound-to-a-data你還在尋找答案這裏? – 2010-06-16 21:55:21

+0

是的,我是,你不客氣。 – 2010-06-16 22:09:56

回答

0

我只是前幾天有類似的問題。

儘量不要更改 CellFormatting事件,但只有Value

爲了避免接收FormatException然後在運行時,你必須轉換/當你想它

你可以存儲回到這個在CellParsing事件解析值,你可以在這裏將您desidered可見格式YYWW,d在接受DateTime格式爲mm/dd/yyyy的

也許看看這個答案可以幫助:DataGridView Cell Editing issue with decimal/hexadecimal formatting

+0

好的提示,我會在接下來的幾天裏測試一下......謝謝! – 2010-06-18 13:15:19

相關問題