无法获取数据做Gridview

时间:2008-12-16 22:33:42

标签: .net asp.net vb.net

我正在尝试将数据集检索到Gridview,但我只是在Gridview中没有得到任何行。我做错了什么?

页面代码

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    CType(Master, AreaTrabalho).AlteraTitulo = "Projectos"

    Using oSQL As New clsSQL(System.Configuration.ConfigurationManager.AppSettings("ConnectionString1"))
        If oSQL.OpenConnection Then
            oSQL.ToDataGrid(Me.GridView1, "Select * from users")
        End If
    End Using
End Sub

用于获取数据的类函数

Public Function ToDataGrid(ByVal oDataGrid As GridView, _
                           ByVal sQuery As String, _
                  Optional ByVal sTable As String = "") As Boolean
    Try

        Dim objDataSet As New Data.DataSet
        'Preenche o dataset
        objDataSet = ToDataSet(sQuery, sTable)

        oDataGrid.DataSource = objDataSet.Tables(0)

        objDataSet.Dispose()
        objDataSet = Nothing

        Return True
    Catch ex As Exception
        RaiseEvent OnError("ToDataGrid", ex)
    End Try
End Function

Public Function ToDataSet(ByVal sQuery As String, Optional ByVal sTable As String = "") As Data.DataSet
    Try

        m_objCommand = New SqlCommand(sQuery, m_objConnection)

        Dim objDataSet As New Data.DataSet
        Dim objSqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(m_objCommand)

        'Verifica se foi defenido a tabela
        If sTable = "" Then
            objSqlDataAdapter.Fill(objDataSet)
        Else
            objSqlDataAdapter.Fill(objDataSet, sTable)
        End If

        objSqlDataAdapter.Dispose()
        objSqlDataAdapter = Nothing

        Return objDataSet
    Catch ex As Exception
        RaiseEvent OnError("ToDataSet", ex)
        Return Nothing
    End Try
End Function

由于

2 个答案:

答案 0 :(得分:2)

在ToDataGrid方法中设置数据源后,您没有在GridView上调用the Databind method

oDataGrid.DataBind()

答案 1 :(得分:2)

我假设这是一个webforms应用程序,如果是这种情况,您需要在设置数据源以将数据绑定到网格后调用GridView.DataBind()