VB.net 对于不考虑第一个数据行的每个循环
VB.net For Each Loop Not Accounting for First Datarow
我目前正在循环访问 Visual Studio 中的数据网格视图,循环运行良好,代码几乎可以满足我的要求,
我唯一的问题是它似乎跳过了,或者没有说明我的 gridview 中的第一行,我已经在网上查找解决这个问题但找不到任何东西,
我想知道我是否可以得到一些帮助...
这是我的循环代码:
'Checks the flags for montor/expiry date to set red or green cells
'------------------------------------------------------------------
For Each Row As GridViewRow In GridView3.Rows
'-----------------------------------------------------------------------------------------
Dim MonitorVal As String = GridView3.DataKeys(e.Row.RowIndex).Values("Monitor").ToString
If MonitorVal.Contains("Yes") Then
Dim IsExtention As String = GridView3.DataKeys(e.Row.RowIndex).Values("FileName").ToString
If IsExtention = "" Or IsExtention = Nothing Or IsExtention = "Null" Then
'set red
'--------
e.Row.BackColor = Drawing.Color.LightPink
Else
'else green
'-----------
e.Row.BackColor = Drawing.Color.LightGreen
End If
Else
'else green
'-----------
e.Row.BackColor = Drawing.Color.LightGreen
End If
Next
我猜想"first row in gridview"其实就是网格的header-row。这不是从 GridView.Rows
返回的,只返回具有 DataControlRowType.DataRow
的行。您只能在 RowDataBound
或 RowCreated
等事件中获得它。我建议在这种情况下使用 RowDataBound
:
Protected Sub GridView3_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView3.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.Header
' ... '
Case DataControlRowType.DataRow
' ... '
Case DataControlRowType.Footer
' ... '
End Select
End Sub
Only rows with their RowType
property set to
DataControlRowType.DataRow
are stored in the Rows
collection. The
GridViewRow objects that represent the header, footer, and pager rows
are not included in the collection.
我目前正在循环访问 Visual Studio 中的数据网格视图,循环运行良好,代码几乎可以满足我的要求,
我唯一的问题是它似乎跳过了,或者没有说明我的 gridview 中的第一行,我已经在网上查找解决这个问题但找不到任何东西,
我想知道我是否可以得到一些帮助...
这是我的循环代码:
'Checks the flags for montor/expiry date to set red or green cells
'------------------------------------------------------------------
For Each Row As GridViewRow In GridView3.Rows
'-----------------------------------------------------------------------------------------
Dim MonitorVal As String = GridView3.DataKeys(e.Row.RowIndex).Values("Monitor").ToString
If MonitorVal.Contains("Yes") Then
Dim IsExtention As String = GridView3.DataKeys(e.Row.RowIndex).Values("FileName").ToString
If IsExtention = "" Or IsExtention = Nothing Or IsExtention = "Null" Then
'set red
'--------
e.Row.BackColor = Drawing.Color.LightPink
Else
'else green
'-----------
e.Row.BackColor = Drawing.Color.LightGreen
End If
Else
'else green
'-----------
e.Row.BackColor = Drawing.Color.LightGreen
End If
Next
我猜想"first row in gridview"其实就是网格的header-row。这不是从 GridView.Rows
返回的,只返回具有 DataControlRowType.DataRow
的行。您只能在 RowDataBound
或 RowCreated
等事件中获得它。我建议在这种情况下使用 RowDataBound
:
Protected Sub GridView3_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView3.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.Header
' ... '
Case DataControlRowType.DataRow
' ... '
Case DataControlRowType.Footer
' ... '
End Select
End Sub
Only rows with their
RowType
property set toDataControlRowType.DataRow
are stored in theRows
collection. The GridViewRow objects that represent the header, footer, and pager rows are not included in the collection.