简体   繁体   English

错误的行在绑定到DataAdapter的DataGridView中被删除

[英]Wrong row gets deleted in DataGridView bound to DataAdapter

Situation : 情况

I'm writing a Winforms app using C# in VS2013 with .NET4.0. 我正在使用.NET4.0在VS2013中使用C#编写Winforms应用程序。

I have a datagridview which I bind to a DataAdapter linked to a MySQL table. 我有一个datagridview,它绑定到链接到MySQL表的DataAdapter。 I'm performing updates using the DataAdapter's Update method. 我正在使用DataAdapter的Update方法执行更新。 Due to the unconventional handling of dates and booleans in the MySQL table I set my own SQL commands rather than using CommandBuilder. 由于对MySQL表中日期和布尔值的非常规处理,我设置了自己的SQL命令,而不是使用CommandBuilder。 These are fed by parameters set before calling Update. 这些由调用Update之前设置的参数提供。 I call Update on RowValidated. 我将更新称为RowValidated。

For inserting and amending this works fine. 对于插入和修改,效果很好。

Issue : 问题

In the datagridview, when the user selects a single row, I want them to be able to hit delete and have the row removed from both the grid and the database. 在datagridview中,当用户选择一行时,我希望他们能够单击Delete并将该行从网格和数据库中删除。 When I try this the row disappears from the datagridview and of course the cursor moves to a different row. 当我尝试此操作时,该行将从datagridview中消失,当然光标会移动到另一行。 But the database row that gets deleted is the new datagridview row that the cursor has fallen on. 但是被删除的数据库行是光标所在的新的datagridview行。 This is almost certainly because I use datagridview.CurrentRow to set the parameter for the delete command and I assume that at this stage the current row has indeed changed. 这几乎可以肯定是因为我使用datagridview.CurrentRow设置了delete命令的参数,并且我认为在这一阶段,当前行确实发生了变化。

Question : 问题

How do I amend this arrangement so the delete will work correctly? 如何修改此安排,以便删除可以正常工作? Clearly the underlying issue is that the row has disappeared from the grid before the database update takes place but I'm not sure how to resolve that. 显然,潜在的问题是在数据库更新发生之前该行已从网格中消失,但是我不确定如何解决该问题。 I suspect the solution may lie in handling things through different events but I'm not sure which. 我怀疑解决方案可能在于通过不同事件处理事情,但是我不确定是哪一个。

Ok generating the SQL Delete command doesn't work when I reference CurrentRow from the dgv because by the time I'm building my parameters and doing the adapter.Update that dgv row has gone. 当我从dgv引用CurrentRow时,无法生成SQL Delete命令不起作用,因为在我建立参数并进行适配器时,更新了dgv行。 That's why I end up deleting the wrong row. 这就是为什么我最终删除错误的行。 So I've solved it like this: 所以我已经这样解决了:

  • In the UserDeletingRow event I'm storing the primary key value for the row being deleted. 在UserDeletingRow事件中,我正在存储要删除的行的主键值。
  • In RowValidated, where I call the adapter.Update I first use the stored primary key to build the correct SQL Delete command. 在RowValidated中,我将其称为adapter.Update。首先,我使用存储的主键来构建正确的SQL Delete命令。 Even though the row has already gone from the dgv the database delete now works correctly. 即使该行已经从dgv中删除,数据库删除现在也可以正常工作。

Relieved to get this working but this does seem like a pretty inelegant solution. 放心地使此工作正常进行,但这似乎是一个非常优雅的解决方案。 Grateful if anyone knows of a slicker way of handling this. 如果有人知道处理此问题的巧妙方法,将不胜感激。 I'd be intrigued to know just what logic the CommandBuilder would have used. 我很想知道CommandBuilder应该使用什么逻辑。 (Sadly I can't use CommandBuilder because my Inserts and Updates need to do Unix_TIMESTAMP conversions). (由于我的插入和更新需要进行Unix_TIMESTAMP转换,因此我无法使用CommandBuilder。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM