使用datagridview从数据库中永久删除行

时间:2017-06-18 15:31:20

标签: vb.net winforms

在VB.net中如何通过在datagridview中选择并使用按钮删除来永久删除数据库中的行

我尝试使用DataGrid.Rows.Remove,但它只从数据网格而不是数据库中删除

我正在使用SQL Server数据库

2 个答案:

答案 0 :(得分:0)

如果没有更多代码,这很困难,但通常网格与数据源相关联。数据源具有deletequery的属性。

来自TechNet的示例:

<%@Page  Language="VB" %>
<%@Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
 Sub On_Record_Deleting(ByVal source As Object, ByVal e As SqlDataSourceCommandEventArgs)
     ' Cancel the delete operation if the checkbox is not checked.
     If Not CheckBox1.Checked 
            e.Cancel = True
            Label1.Text = "The command was cancelled because the CheckBox was not checked."
     End If

End Sub 'On_Record_Deleting

Sub On_Record_Deleted(ByVal source As Object, ByVal e As SqlDataSourceStatusEventArgs)
    Label1.Text = e.AffectedRows & " row(s) were deleted"

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

        <asp:SqlDataSource
            id="SqlDataSource1"
            runat="server"
            DataSourceMode="DataSet"
            ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
            SelectCommand="SELECT * FROM Orders"
            DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;"
            OnDeleting="On_Record_Deleting"
            OnDeleted="On_Record_Deleted">
        </asp:SqlDataSource>
        <br />

       <asp:CheckBox 
         id="CheckBox1" 
         runat="server"
         autopostback="true"
         text="Check To Delete Data" />
        <br />
        <br />

        <asp:GridView
            id="GridView1"
            runat="server"
            AutoGenerateColumns="False"
            DataKeyNames="OrderID"
            AutoGenerateDeleteButton="True"
            AllowPaging="True"
            PageSize="20"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField HeaderText="Order ID" DataField="OrderID" />
                <asp:BoundField HeaderText="Customer" DataField="CustomerID" />
                <asp:BoundField HeaderText="Order Placed" DataField="OrderDate" />
                <asp:BoundField HeaderText="Order Shipped" DataField="ShippedDate" />
            </Columns>
        </asp:GridView>

        <asp:Label
            id="Label1"
            runat="server">
        </asp:Label>

    </form>
  </body>
</html>

答案 1 :(得分:0)

根据您的原始帖子和后续评论,我认为您的表单看起来像

Screenshot - Form with a DataGridView created using add DataSource

如果是这种情况,您可以在不添加代码的情况下永久删除。这种方式的工作方式是允许您编辑,删除和添加许多行。它默认具有支持数据集,并且具有更改跟踪功能。完成后,单击保存图标,它将提交对数据库的更改。

如果您已经添加了自己的界面,则必须查看生成的控件以了解该怎么做。您使用的方法会在其他项目中创建支持DataSetDataAdapter以及DataTable

' You should have a binding source - I used a table called PointsOfInterest
Me.PointsOfInterestBindingSource.EndEdit()  ' changes that have been tracked will be applied to the datasource
Me.TableAdapterManager.UpdateAll(Me.Your_DataSet)

这应该将您的更改提交回数据库。