开发者

how to delete row from related child table

I've created a co开发者_Python百科mbobox for navigating orders in a grid view.

    Dim adapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Customers", cstr)
    Dim adapter2 As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Orders", cstr)
    adapter.Fill(ds, "Customers")
    adapter2.Fill(ds, "Orders")
    ds.Relations.Add("CustOrd", ds.Tables("Customers").Columns("CustomerID"), ds.Tables("Orders").Columns("CustomerID"))

    bs = New BindingSource(ds, "Customers")

    ComboBox1.DataSource = bs
    ComboBox1.DisplayMember = "CompanyName"
    ComboBox1.ValueMember = "CustomerID"

    DataGridView1.DataSource = bs
    DataGridView1.DataMember = "CustOrd"

Now I want to add delete button for grid's selected row in Orders table. But I have some misunderstanding on how it should works for this data relation.

If I use BindingSource.RemoveAt(index) it removes all rows for whole selected CustomerID and don't saves changes actually. I'm not experienced enough, could you give me an example for my code, how to delete row from Orders db.

UPDATE: So, I created this for deletion, but stil have problems:

    Dim key As String = DataGridView1.SelectedRows(0).Cells("OrderID").Value.ToString
    Dim dr As DataRow = ds.Tables("Orders").Rows.Find(key)
    dr.Delete()
    adapter2.Update(ds, "Orders")

Err: The DELETE statement conflicted with the REFERENCE constraint "FK_Order_Details_Orders".

Yes I did, but when I delete all orders for customer and click on a grid I get -1 in row. Why?


You're just deleting the orders. You need to also delete the Order Details, which is a child table of Orders.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜