【问题标题】:How to delete DataRow from DataTable on RowCommand如何从 RowCommand 上的 DataTable 中删除 DataRow
【发布时间】:2012-04-09 11:05:13
【问题描述】:

我正在尝试从数据表中删除网格视图中选定的行,但它似乎没有删除。如果语句 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

【问题讨论】:

    标签: vb.net gridview datatable


    【解决方案1】:

    indexTable.Rows(index).Delete() 中来自哪里?

    不管怎样,这个Delete() 只标记了要删除的记录在内存中。您需要添加 Adapter.Update() 才能将其从数据库中删除。

    【讨论】:

    • 感谢您引导我找到答案。 Table.Rows(index).Delete() Table.AcceptChanges()
    猜你喜欢
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    相关资源
    最近更新 更多