更新后如何重新绑定网格视图?

How to rebind grid view after update?

我可以知道如何在更新后将数据重新绑定到网格视图table吗?以下是我到目前为止所做的尝试:

    Dim conn As New MySqlConnection
    conn.ConnectionString = "server = localhost; user id = root; password = root; database = db_fyp"
    Dim com As New MySqlCommand
    Dim dt As New DataTable

    Dim query As String

    query = "update tblorder set OrderStatus = @OrderStatus where UserId = @UserId "

    com = New MySqlCommand(query, conn)

    com.Parameters.AddWithValue("@OrderStatus", tOrderStatus)
    com.Parameters.AddWithValue("@UserId", Session("Username"))

    conn.Open()

    com.ExecuteNonQuery()

    BindData()

    conn.Close()

这是我的 BindData 函数,用于将数据插入网格视图 table。这部分工作完美。

Private Sub BindData()
    Dim conn As New MySqlConnection
    conn.ConnectionString = "server = localhost; user id = root; password = root; database = db_fyp"
    Dim com As New MySqlCommand
    Dim dr As MySqlDataReader
    Dim query As String
    Dim dt As New DataTable

    conn.Open()

    query = "select FoodName, Qty, IngredientName, Quantity, OrderStatus from tblorder, tblorderdetail, tblfood, tblcustomizefooddetail, tblcustomizeingredient, tblordercustomize where UserId = @UserId and OrderStatus = @OrderStatus and tblorder.OrderId = tblorderdetail.OrderId and tblorderdetail.FoodId = tblfood.FoodId and tblorderdetail.OrderDetailId = tblordercustomize.OrderDetailId and tblfood.FoodId = tblcustomizefooddetail.FoodId and tblcustomizeingredient.IngredientId = tblcustomizefooddetail.IngredientId and tblordercustomize.IngredientId = tblcustomizeingredient.IngredientId"

    com = New MySqlCommand(query, conn)

    com.Parameters.AddWithValue("@UserId", Session("Username"))
    com.Parameters.AddWithValue("@OrderStatus", "Pending")

    dr = com.ExecuteReader

    dt.Load(dr)
    Session("OderTable") = dt
    GridView1.DataSource = dt
    GridView1.DataBind()
    conn.Close()
End Sub

您已经有填充网格的函数,只需在更新代码中调用它即可:

Dim conn As New MySqlConnection
conn.ConnectionString = "server = localhost; user id = root; password = root; database = db_fyp"
Dim com As New MySqlCommand
Dim dt As New DataTable
Dim query As String
query = "update tblorder set OrderStatus = @OrderStatus where UserId = @UserId "
com = New MySqlCommand(query, conn)
com.Parameters.AddWithValue("@OrderStatus", tOrderStatus)
com.Parameters.AddWithValue("@UserId", Session("Username"))
conn.Open()
com.ExecuteNonQuery()
conn.Close()

'you dont need all that
'dt.Load(????)
'GridView1.DataSource = dt
'GridView1.DataBind()

'just call this
BindData()