我正在循环浏览Visual Studio中的数据网格视图,The Loop运行良好,代码完全符合我的要求,
我唯一的问题是它似乎跳过了,或者没有考虑到我的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
答案 0 :(得分:1)
我猜“gridview中的第一行”实际上是指网格的标题行。这不是从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
仅将
RowType
属性设置为的行DataControlRowType.DataRow
存储在Rows
集合中。该 GridViewRow对象,表示页眉,页脚和寻呼机行 不包括在集合中。