2014-01-24 57 views
1

我在DataGridView中有一個單元格,我在這種格式(hh:mm)中寫了一個小時,但是當我嘗試將其轉換爲DateTime值時,它給了我這個(01-01-001 00 :00:00)。誰能告訴我我做錯了什麼?轉換爲DateTime錯誤

DateTime hora; 
hora = Convert.ToDateTime(dataGridView1.Rows[1].Cells[1].Value); 
+0

dataGridView1.Rows [1] .Cells [1] .Value'中的確切字符串/數據是什麼? –

+0

我試過這個11:23例如。 – user3150130

回答

1

您可能需要使用DateTime.ParseExact以及適當的格式說明符。

請注意,如果使用小時/分鐘作爲格式,結果DateTime的日期部分將不會被設置,並且很可能是您應該忽略的內容。

如果小時和分鐘僅表示一段時間,請考慮使用TimeSpan代替DateTime

0

11:23不是有效的DateTime,它也沒有Year,Month和Day,那是一個TimeSpan。使用以下內容:

TimeSpan thetime; 
TimeSpan.TryParse(dataGridView1.Rows[1].Cells[1].Value, out thetime);