如何在RowCommand上从DataTable中删除DataRow

时间:2012-04-09 11:05:14

标签: vb.net gridview datatable

我正在尝试从数据表中删除gridview中的选定行,但它似乎没有删除。如果statment Table.Rows.Count = 0不起作用,我在网格视图中尝试一行。

Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
    Dim index As Integer = Convert.ToInt32(e.CommandArgument)

    Dim Table As New DataTable
    Table = GridView1.DataSource
    Dim iOriPrice As Double
    iOriPrice = Table.Rows(index).Item("sPrice")
    Table.Rows(index).Delete()
    'reset table and set to gridview
    If Table.Rows.Count = 0 Then
        Table.Reset()
        GridView1.DataSource = Table
        GridView1.DataBind()
        lblTotalAm.Text = ""
        lblTotalMsg.Text = "Shopping Cart is empty"
        Session.Add("BuyTable", Table)
        btnBuyNow.Visible = False
    Else
        'calculate total sum and 
        Dim newTotal As Double
        newTotal = Convert.ToDouble(lblTotalAm.Text) - iOriPrice
        lblTotalAm.Text = newTotal
        Session.Add("BuyTable", Table)
        GridView1.DataSource = Table
        GridView1.DataBind()
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

index来自Table.Rows(index).Delete()

无论如何,这个Delete()仅标记了内存中删除的记录。您需要添加Adapter.Update()以从数据库中删除它。