2015-01-07 41 views
0

試圖盡我所能地解釋這一點。C#datagridview比較每行的單元格值

我有一個datagridview。我有3行7個單元格。在第七個單元格中存儲DateTimePicker值。我想比較每行的第7個單元格的值和某個日期。如果第7個單元格的日期值高於我希望與之比較的日期,則用紅色對行進行着色。

我走到這一步:

foreach(DataGridViewRow r in dataGridView1.Rows) 
{ 
    if(dont know what to type here) 
     { 
     r.DefaultCellStyle.BackColor = Color.Red; 
     } 
} 

回答

1

你需要提取出7細胞Value和轉換/它轉換爲DateTime那麼你可以比較一下,如:(記住電池是基於0,因此第7單元將在r.Cells[6]

DateTime yourCompareDate = DateTime.Now; 
foreach (DataGridViewRow r in dataGridView1.Rows) 
{ 
    DateTime cellValue7 = Convert.ToDateTime(r.Cells[6].Value); //Convert/Cast value to date 
    if(cellValue7 > yourCompareDate) 
    { 
     r.DefaultCellStyle.BackColor = Color.Red; 
    } 
} 
0

試試這個:

var compareDate = DateTime.Now; //or whatever date you want 
foreach(DataGridViewRow r in dataGridView1.Rows) 
    { 
     var cellDate = Convert.TodateTime(r.Cells[6].Value); 
     if (cellDate > compareDate) r.DefaultCellStyle.BackColor = Color.Red;  
    } 
0
 DateTime dtValue = DateTime.Now; // You Date Time Value 
     foreach (DataGridViewRow r in dataGridView1.Rows) 
     { 
      if ((DateTime)r.Cells[6].Value > dtValue) 
      { 
       r.DefaultCellStyle.BackColor = System.Drawing.Color.Red; 
      } 
     } 
+0

這是一般的想法,但請把更具體的細節。我的意思是,請記住關於查找高於該值的日期時間的問題。 – ryanyuyu

相關問題