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

时间:2015-01-13 17:18:46

标签: asp.net vb.net datagridview

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

    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函数,用于将数据插入到网格视图表中。这部分工作得很好。

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

1 个答案:

答案 0 :(得分:1)

你已经有了填充网格的功能,只需在更新代码中调用它:

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()