打印本文 打印本文 关闭窗口 关闭窗口
How to Confirm a Delete in an ASP.NET Datagrid...
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1824  更新时间:2009/4/23 10:45:00  文章录入:mintao  责任编辑:mintao
l behavior since I am not really deleting the rows from the database, only from the dataset. If you uncomment out the lines where noted, the deletes will actually occur in the database also.

  Public Sub Delete_Row(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)

    '''' Retrieve the ID of the product to be deleted
    Dim ProductID As system.Int32 = System.Convert.ToInt32(E.Item.Cells(0).Text)

    dtgProducts.EditItemIndex = -1

    '''' Create and load a DataSet
    Connect()
    Dim adapter As New SqlDataAdapter(strSql, objConn)
    Dim ds As New DataSet()
    adapter.Fill(ds, "Products")
    Disconnect()

    '''' Mark the product as Deleted in the DataSet
    Dim tbl As DataTable = ds.Tables("Products")
    tbl.PrimaryKey = New DataColumn() _
                    { _
                      tbl.Columns("ProductID") _
                    }
    Dim row As DataRow = tbl.Rows.Find(ProductID)
    row.Delete()

    '''' Reconnect the DataSet and delete the row from the database
    ''''-----------------------------------------------------------
    '''' Following section commented out for demonstration purposes
    ''''Dim cb As New SqlCommandBuilder(adapter)
    ''''Connect()
    ''''adapter.Update(ds, "Products")
    ''''Disconnect()
    ''''-----------------------------------------------------------
    '''' Display remaining rows in the DataGrid
    dtgProducts.DataSource = ds.Tables("Products")
    dtgProducts.DataBind()

  End Sub

End Class

You may run the program here.
You may download the code here.

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口