如何刷新gridview?

时间:2011-12-26 15:23:16

标签: asp.net gridview

我是使用asp.net的新手,我在更新数据后如何刷新GridView时遇到问题,但似乎它没有在我的其他页面上工作。当我更新供应商信息时,我有相同的代码,然后GridView1.Databind()正在运行,但当我尝试在我的其他页面上再次使用它时,它不起作用。你能说出为什么会这样吗?

这是我的代码:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim cmd As New SqlCommand

        cmd.Connection = cn
        cmd.CommandText = "UPDATE ProductTable SET ProductCode = ('" & lbl_productcode.Text & "'), ProductName = ('" & txt_prodname.Text & "'),ProductCategory =('" & lbl_category.Text & "'),Price =('" & txt_price.Text & "'), Quantity=('" & txt_qty.Text & "'), CategoryID=('" & lbl_catid.Text & "') WHERE ProductCategory = '" & TextBox1.Text & "'"
        cmd.Connection.Open()

        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
        MsgBox("RECORD UPDATED", MsgBoxStyle.Information)
        GridView1.DataBind()
        Call clear()
    End Sub

1 个答案:

答案 0 :(得分:1)

我在您的代码中没有看到您DataSource之前GridView实际设置DataBind()的{​​{1}}。看看吧!

更新:

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim cmd As New SqlCommand

    cmd.Connection = cn
    cmd.CommandText = "UPDATE ProductTable SET ProductCode = ('" & lbl_productcode.Text & "'), ProductName = ('" & txt_prodname.Text & "'),ProductCategory =('" & lbl_category.Text & "'),Price =('" & txt_price.Text & "'), Quantity=('" & txt_qty.Text & "'), CategoryID=('" & lbl_catid.Text & "') WHERE ProductCategory = '" & TextBox1.Text & "'"
    cmd.Connection.Open()

    Me.GridView1.DataSource = cmd.ExecuteReader()
    GridView1.DataBind()

    cmd.Connection.Close()
    MsgBox("RECORD UPDATED", MsgBoxStyle.Information)

    Call clear()
End Sub
祝你好运!

相关问题