Deleting rows in a child DataViewGrid
January 23rd, 2008 | by Paul Grossman |When working with VB 2008, I set up a Parent/Child grids on a Windows form using LINQ to SQL.
What happened is when I try to delete a child row (i.e. An “order” for a given “Customer” where customer is the parent and Order is the child), I get a message similar to this: “An attempt was made to remove a relationship between a Customer and a Order. However, one of the relationship’s foreign keys (Order.CustomerID) cannot be set to null”.
I regularly read Beth Massi’s blog (Beth is a program manager with Microsoft, and has a great series of “how-to” videos using VB, LINQ and a few other technologies - it’s great you should check it out at http://msdn2.microsoft.com/en-us/vbasic/bb466226.aspx?wt.slv=topsectionsee). Beth is extremely good at being able to explain “how-to’s” when it comes to technical items such as VB - she breaks everything down into an easy to follow step-by-step format. Also, it’s really nice that someone so proficient focuses on VB and LINQ too. Anyhow Beth has a blog posting that addresses this exact problem and it solved everything here for me.
So instead of going into a long and drawn-out explanation of what I did, I’m just going to give the link to Beth’s blog post where she does an outstanding job explaining the problem and the solution. Make sure to check out her blog posting here: http://blogs.msdn.com/bethmassi/archive/2007/10/02/linq-to-sql-and-one-to-many-relationships.aspx
By the way I resolved my issue as follows: In addition to setting DeleteOnNull=”‘true” for the child table in the .dbml file, I also used the option to enable cascading deletes on the parent. So this way, I can now just delete child rows (without having to delete the parent row) or, when I delete a parent row, all the child rows are automatically deleted too (this is OK in my application because I’m not really working with orders - so in my case when I delete the parent, I really do want all the children rows to be deleted too).
Thanks goes out to Beth Massi - you’re a great help !
You must be logged in to post a comment.