2016-09-01 34 views
0

我遇到問題。如果時間是午夜,DataGridView將僅顯示日期。首先,我認爲這是因爲我的LINQ查詢是dataGridView的數據源。我無法更改查詢,所以我決定從代碼中添加零。這是我的代碼:dataGridView顯示2016-09-01而不是2016-09-01 00:00

  for (int i = 0; i < dataGridViewRaporty.Rows.GetRowCount(DataGridViewElementStates.Visible); i++) 
      { 
       for (int j = 0; j < dataGridViewRaporty.Columns.GetColumnCount(DataGridViewElementStates.Visible); j++) 
       { 
        string wartoscKomorki = dataGridViewRaporty.Rows[i].Cells[j].Value.ToString(); 
        if (wartoscKomorki.Contains("-") && wartoscKomorki.Length == 10){ 
         wartoscKomorki = wartoscKomorki + " 00:00"; 
         dataGridViewRaporty.Rows[i].Cells[j].Value = wartoscKomorki.ToString(); 
        } 
       } 
      } 

我很驚訝,因爲沒有添加零。然後我在MessageBoxes中顯示這些值,並在那裏顯示零。結論是,dataGridView已獲得這些值爲零,但不顯示它們。有沒有解決方案可以顯示完整的DateTime格式?

+0

那麼你的if子句失敗?使用調試器!另外:沒有格式字符串的ToString不受控制;爲什麼? – TaW

+0

這並不重要,因爲dataGridView已經獲得了這個零值,只是不顯示它們。 – Artur

+0

要控制顯示的內容,請使用格式字符串! – TaW

回答

0

正如我所說的一切都是因爲dataGridView的行爲。這是解決方案:

dataGridViewRaporty.Columns[3].DefaultCellStyle.Format= "yyyy-MM-dd HH:mm";