声明gridview selectcommand

时间:2010-11-09 19:00:43

标签: .net asp.net sql vb.net gridview

当页面加载时,应该使用我的第一个SQL SELECT语句加载GridView(位于更新面板中)。

在按钮单击上,同一个GridView将加载新数据。

我做错了什么?启动时,GridView不会出现。

Public cmd As New SqlCommand()
    Public percentp As New SqlCommand()
    Public da As New SqlDataAdapter(cmd)
    Public conn As New SqlConnection("Data Source=TEST-TEST-TEST01;Initial Catalog=TEST;Integrated Security=True")

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    cmd.Connection = conn
    conn.Open()
    Dim ds As New DataSet
    cmd.CommandText = "Select * from test1"
    da.Fill(ds)
    GridView1.DataSource = ds.Tables(0)
    da.FillSchema(ds, SchemaType.Mapped)
    conn.Close()        
End Sub   

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    cmd.Connection = conn
    conn.Open()
    Dim ds As New DataSet
    cmd.CommandText = "SELECT * from test"
    da.Fill(ds)
    GridView1.DataSource = ds.Tables(0)
    da.FillSchema(ds, SchemaType.Mapped)
    conn.Close()        
    TextBox1.Text="aaaaaaaaaaa"
End Sub
End Class

我试过了    GridView1.DataSource = ds.Tables(0)    GridView1.DataBind

3 个答案:

答案 0 :(得分:1)

您需要致电

GridView1.DataSource = ds.Tables(0)
GridView1.DataBind() // this is needed to bind the datasource to GridView

答案 1 :(得分:1)

您需要在设置数据源后调用GridView1.DataBind();

答案 2 :(得分:1)

从数据库中检索数据后,您忘记包含GridView1.Databind()命令。您通常在检索数据后设置数据源,因此您的Page_Load方法可以更好地工作:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    cmd.Connection = conn
    conn.Open()
    Dim ds As New DataSet
    cmd.CommandText = "Select * from test1"
    da.Fill(ds)
    conn.Close()        
    GridView1.DataSource = ds.Tables(0)
    GridView1.DataBind()        
End Sub