在MultiPage Gridview中获取行数?

时间:2012-01-31 15:04:48

标签: vb.net gridview label datasource rowcount

当我尝试执行以下操作时:

lblTotal.text  = gwGrid.rows.count()

我总是得到50,这是我页面的大小。如何获取返回的所有记录的计数,而不仅仅是那一页上显示的记录?

我还在我的数据源上尝试了Selected事件:

Protected Sub ObjectDataSource1_Selected(ByVal sender As Object, ByVal e As ObjectDataSourceStatusEventArgs)

    If e.Exception Is Nothing AndAlso e.ReturnValue IsNot Nothing Then
        Dim dt As DataTable = TryCast(e.ReturnValue, DataTable)
        Dim totalRecordCount As Integer = dt.Rows.Count
    End If

End Sub

但是我收到以下错误:

对象引用未设置为对象的实例。 在这条线上: 第85行:Dim totalRecordCount As Integer = dt.Rows.Count

udpate:我明白了:

 Protected Sub ObjectDataSource1_Selected(ByVal sender As Object, ByVal e As ObjectDataSourceStatusEventArgs)
        If e.Exception Is Nothing Then
            Dim dt As DataSet = DirectCast(e.ReturnValue, DataSet)
            If dt IsNot Nothing Then
                lblTotal.Text = dt.Tables(0).Rows.Count.ToString()
            Else
                lblTotal.Text = "0"
            End If
        End If
    End Sub

2 个答案:

答案 0 :(得分:2)

如果您正在使用SqlDataSource寻找此问题的答案,请使用相同的事件,但只需使用e.AffectedRows进行计数。

Protected Sub WorkerData_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles WorkerData.Selected

    If e.Exception Is Nothing Then

        Dim rows As Integer = e.AffectedRows

        'AddMessage(rows.ToString & " Workers selected", UpdateMessage)

    End If

End Sub

这对我很有用。

答案 1 :(得分:0)

Protected Sub ObjectDataSource1_Selected(ByVal sender As Object, ByVal e As ObjectDataSourceStatusEventArgs)
        If e.Exception Is Nothing Then
            Dim dt As DataSet = DirectCast(e.ReturnValue, DataSet)
            If dt IsNot Nothing Then
                lblTotal.Text = dt.Tables(0).Rows.Count.ToString()
            Else
                lblTotal.Text = "0"
            End If
        End If
    End Sub