每个循环的VB.net不考虑第一个Datarow

时间:2015-05-12 09:38:12

标签: asp.net vb.net for-loop gridview

我正在循环浏览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

1 个答案:

答案 0 :(得分:1)

我猜“gridview中的第一行”实际上是指网格的标题行。这不是从GridView.Rows返回的,只返回DataControlRowType.DataRow行。您只能在RowDataBoundRowCreated等活动中获取此信息。我建议在这种情况下使用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

Documentation

  

仅将RowType属性设置为的行   DataControlRowType.DataRow存储在Rows集合中。该   GridViewRow对象,表示页眉,页脚和寻呼机行   不包括在集合中。