【问题标题】:Display Row Number in Visible Gridview Rows在可见的 Gridview 行中显示行号
【发布时间】:2014-08-27 00:36:10
【问题描述】:

如何在 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

【问题讨论】:

  • 您遇到错误了吗?
  • no.. 但如果我把它放在 If row.visible 条件中,它不会显示行数。当我删除 If 条件时,它会显示但计数不正确。
  • 不确定我是否了解您的需求。你只是想对可见行进行编号吗?你能发布你的 HTML 吗?
  • "lblNum" 是 asp 模板的一部分吗?
  • @Cory 我添加了我的网格视图的屏幕截图

标签: asp.net vb.net gridview


【解决方案1】:

这样试试

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

【讨论】:

    猜你喜欢
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 2011-05-07
    • 2012-03-14
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    相关资源
    最近更新 更多