vb.net dataview grid不会添加记录,独立修改数据后不更新

发布于 2025-01-11 21:24:29 字数 1370 浏览 0 评论 0原文

我有一个在运行时绑定到数据源的数据视图网格。数据源通过 DataAdapter 从访问数据库填充。数据填充和显示正确,并且对现有行的更新似乎工作正常,但我有两个问题:

  1. 当我在新行中键入内容然后按回车键或切换到不同的行时,我希望 DataAdapter 添加该行,然后并保存到数据库中,以便我可以从 Access 检索新记录的自动编号索引,并使用它在不同的表(条目、多对多链接表)中添加关联记录。这并没有发生。在RowLeave事件中,我有adapter.Update(dsSentences),然后检查新行,但是RowCount没有反映它的存在,即使新添加的数据在网格中可见,并且adapter.Update也没有似乎触发了我在 DataAdapter 中指定的插入查询。所以没有添加任何东西。 (编辑:好的,所以当此事件被触发时,新行尚未添加。然后我应该使用哪个事件来提交数据并检索新记录的自动编号主键?我已经尝试过 UserAddedRow 但那个事件被触发在将任何数据输入新行之前。)
  2. 第二个问题是我需要独立更新数据,然后让网格反映这些更改。我该怎么做?是否有一些调用会强制网格通过数据集从 DataAdapter 获取更新的数据?任何帮助将不胜感激。我几乎准备好放弃绑定数据的整个想法并通过代码完成这一切,数据装箱应该可以节省时间,但我发现它迷宫般且不可预测。

FWIW 这是我用来填充网格的查询: 参数 nIdCollection Long; SELECT tblSentences.IdSentence、tblSentences.SentenceText、tblSentences.SentenceParsed、Not IsNull([tblSentences]![SentenceParsed]) AS HasParsed、Entries.IdEntry FROM tblSentences INNER JOIN tblSentences.IdSentence = Entries.IdSentence 上的条目 WHERE (((Entries.IdCollection)=[nIdCollection])) ORDER BY Entries.SortValue;

正如你所看到的,它需要在Entries中记录。在我在 tblSentences 中输入新记录后,在有任何条目之前,假设 IdEntry 完全出现,则 IdEntry 将为 null。这就是为什么我需要在插入后直接拦截,将记录添加到条目并重新查询以保持一切有序。您可以在 SQL 存储过程中完成这一切,但我必须使用 Access。

编辑:经过大量谷歌搜索后,我得出的结论是,我想要做的事情=每次添加新行时,除了 DataAdapter 处理的查询之外,还通过附加的 INSERT 查询将记录添加到表中- 如果您使用数据绑定,则根本无法完成。我将不得不删除所有代码并从头开始通过代码(未绑定)填充网格。我认为这是做我想做的事的唯一方法。我将把这一点留在这里,作为对其他人的警告,当您的数据来自多个表时,不要犯我尝试使用数据绑定的错误。糟糕的错误。

I have a dataview grid bound to a datasource at run time. The datasource is filled from an access database via a DataAdapter. The data fills and displays correctly, and updates to existing rows seem to work OK but I have two problems:

  1. When I type something in a new row and then press return or switch to a different row, I want the DataAdapter to add that row then and there to the database so I can retrieve the Autonumber index of the new record from Access and use that to add an associated record in a different table (Entries, a many to many linking table). This isn't happening. In the RowLeave event I have adapter.Update(dsSentences) and then I check for the new row, but the RowCount doesn't reflect its presence even though the newly added data is visible in the grid, and the adapter.Update doesn't seem to have triggered the Insert query that I specified in the DataAdapter. So nothing is added.
    (edit: OK, so the new row has not yet been added when this event is fired. Which event should I then use to commit the data and retrieve the Autonumber primary key for my new record? I've tried UserAddedRow but that one fires before you've entered any data into the new row.)
  2. THe second problem is that I need to update the data independently and then have the grid reflect those changes. How do I do that? Is there some call that will force the grid to get the updated data from the DataAdapter via the Dataset? Any help would be much appreciated. I'm almost ready to dtop the whole idea of binding data and do it all through code, Data binfing is supposed to save time but I'm finding it labyrinthine and unpredictable.

FWIW here's the query I'm using to fill the grid:
PARAMETERS nIdCollection Long;
SELECT tblSentences.IdSentence, tblSentences.SentenceText, tblSentences.SentenceParsed, Not IsNull([tblSentences]![SentenceParsed]) AS HasParsed, Entries.IdEntry
FROM tblSentences INNER JOIN Entries ON tblSentences.IdSentence = Entries.IdSentence
WHERE (((Entries.IdCollection)=[nIdCollection]))
ORDER BY Entries.SortValue;

As you can see, it requires a record in Entries. After I've entered a new record in tblSentences, before there are any entries the IdEntry will be null assuming it shows up at all. That's why I need to intercept directly after the Insert, add the record to Entries and requery to keep everything in order. You could do it all in an SQL stored procedure but I have to use Access.

Edit: After a lot of googling I've come to the conclusion that what I'm trying to do = add a record to a table through an additional INSERT query apart from the one handled by the DataAdapter, every time a new row is added - simply can't be done if you are using data binding. I am going to have to delete all my code and start from scratch populating the grid through code (unbound). I think it's the only way to do what I want. I will leave this here as a warning to anyone else not to make my mistake of trying to use Data binding when your data is coming from more than one table. Bad mistake.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文