2012-12-18 12 views
0

我想檢查每行的單元格(3)中的日期。如果第一個If條件成立,我只能得到日期,其餘的單元格顯示爲空白。代碼如下。如果語句在循環中的網格視圖

 For Each row As GridViewRow In GridView1.Rows 
      If Not IsDBNull(dr("B1Week").ToString) Then 
       e.Row.Cells(3).Text = dr("B1week").ToString 
       e.Row.Cells(3).ForeColor = System.Drawing.Color.Blue 

      ElseIf DD2 <= Now Then 
       e.Row.Cells(3).Text = e.Row.Cells(3).Text & " - OVERDUE" 
       e.Row.Cells(3).ForeColor = System.Drawing.Color.Red 

      ElseIf DD2 <= Now.AddDays(7) Then 
       e.Row.Cells(3).Text = e.Row.Cells(3).Text & " - DUE " 
       e.Row.Cells(3).ForeColor = System.Drawing.Color.Green 

      End If 

     Next row 

我希望檢查每行中的每個單元格3並分配值。誰能幫忙?

回答

0

裏面你爲每個需要使用行變量循環:

For Each row As GridViewRow In GridView1.Rows 
     If Not IsDBNull(dr("B1Week").ToString) Then 
      row.Cells(3).Text = dr("B1week").ToString 
      row.Cells(3).ForeColor = System.Drawing.Color.Blue 

     ElseIf DD2 <= Now Then 
      row.Cells(3).Text = e.Row.Cells(3).Text & " - OVERDUE" 
      row.Cells(3).ForeColor = System.Drawing.Color.Red 

     ElseIf DD2 <= Now.AddDays(7) Then 
      row.Cells(3).Text = e.Row.Cells(3).Text & " - DUE " 
      row.Cells(3).ForeColor = System.Drawing.Color.Green 

     End If 

    Next row 
+0

能告訴我該怎麼做,因爲目前的第一,如果正在顯示的條件如果爲真。如果第一個if爲false,則單元格顯示爲空白。 – user1764540

+0

你看過上面的代碼嗎?我已經編輯它將e.Row.Cells(3)更改爲row.Cells(3)... –