GridView - 在渲染之前获取每行特定列中单元格的值?

时间:2015-02-16 09:30:45

标签: asp.net vb.net

我有一个填充了数据的gridview。我想在网格视图显示之前评估每一行,以查看该行是否有效显示。在一行的第一列中,我想通过一个返回布尔结果的函数传递它

在伪代码中:

FOR Each row in GridView
IF functionCheckRow(GridView.Rows(i).Cells(0).Text) = False Then
    GridView.Rows(i).Visible = False
End IF
Next

到目前为止,使用ASP / VB.Net我有这个:

Private Sub GridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles  GridView_RowDataBound.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then    'Not the header row?
        Dim index As Integer = e.Row.RowIndex
        Dim currentRow As GridViewRow =  GridView_RowDataBound.Rows(index)


        Dim currentItem = currentRow.Cells(index).Text
        If functionCheckPerms(currentItem ) <> "" Then
            e.Row.Visible = False
        End If
        currentItem = ""
    End If

然而,尝试我似乎无法获得当前行的索引。在这一行:

Dim currentRow As GridViewRow =  GridView_RowDataBound.Rows(index)

我得到:&#34;索引超出范围。必须是非负数且小于集合的大小。 参数名称:index&#34;将值设置为0.我是否以错误的方式进行此操作?

1 个答案:

答案 0 :(得分:0)

这可能是重复的 How can I get the previous row in gridview rowdatabound?

基本上,您需要从行索引中减去1。

Dim index as Integer = e.Row.RowIndex - 1