在可见Gridview行中显示行号

时间:2014-08-27 00:36:10

标签: asp.net vb.net gridview

如何在gridview中显示可见行的行号?我有这个代码(下面),但它不起作用。

Protected Sub grd_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles grd.DataBound
        Dim iNum As Integer = 0
        For Each row As GridViewRow In grd.Rows
            Dim lblNum As Label = TryCast(row.Cells(0).FindControl("lblNum"), Label)
            Dim txtRemark As TextBox = TryCast(row.Cells(10).FindControl("txtRemark"), TextBox)
            If row.RowType = DataControlRowType.DataRow Then
                        If txtRemark.Text = "F" Then
                            row.Visible = False
                        ElseIf txtRemark = "P" Then
                            row.Visible = False
                        End If
            End If
            If row.Visible = True Then
                iNum += 1
                lblNum.Text = iNum
            End If
        Next
    End Sub

The image shows the right numeration in rowindex 0

The rowindex 0 should display 1 instead of 5

1 个答案:

答案 0 :(得分:0)

试试这个

Protected Sub grd_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles grd.DataBound
    Dim iNum As Integer = 0
    For Each row As GridViewRow In grd.Rows     
        Dim txtRemark As TextBox = TryCast(row.Cells(10).FindControl("txtRemark"), TextBox)
        If row.RowType = DataControlRowType.DataRow Then
            If txtRemark.Text = "F" Then
                row.Visible = False
            ElseIf txtRemark.Text = "P" Then
                row.Visible = False
            Else
                iNum += 1
                TryCast(row.Cells(0).FindControl("lblNum"), Label).Text = iNum
            End If
        End If
    Next
End Sub