从 datagridview 更新与数据库对应的数据集

发布于 2024-09-12 00:45:57 字数 282 浏览 2 评论 0原文

有一个datagridview对应于某个数据库的一张表。 该数据库有一个名为“id”的(自动递增)标识列。该列也是主键。 现在,用户只能看到一个空白的 datgridview。他/她可以修改此表(datagridview)。 例如,添加一行、删除一行、编辑(更新)一个单元格,并且可以进行所有可能的修改。

我尝试过使用 sqlcommandBuilder,但没有获得结果。 我该怎么做? 我正在使用 sql server 和 c#。

干杯。

There is a datagridview corresponding to a table of some database.
This database has an (auto-incrimenting) identity column named "id". This column is the primary key, too.
Right now, a user just sees a blank and empty datgridview. He/she can modify this table(datagridview).
For example, add a new row, remove one, edit(update) one cell, and can do every possible modification.

I have tried using sqlcommandBuilder, but no result is obtained.
How can I do this?
I am using sql server and c#.

Cheers.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向日葵 2024-09-19 00:45:57

虽然有很多数据绑定方法(SQLcommandbuilder、objectcommandbuilder 等),但当我控制更多流程时,我总是运气最好。我从旧的 DAO 和 ADO 时代就开始使用它。称其为老式,但您将很快开发出经过测试且易于修改的代码库。

我当前的方法适用于 ASP.NET gridview,但原理是相同的。

  1. 编写填充数据集的数据绑定代码 (ds.Tables(0))
  2. 使用 BoundColumns 构建网格 - 不要让它“自动生成列”。
  3. 添加用于编辑、更新和取消的按钮列。根据需要显示/隐藏列。一次一行处于编辑模式。
  4. 将数据集绑定到网格的数据源。这并不是真正创建返回数据集的链接,只是从数据集中填充静态网格。
  5. 处理编辑、更新和取消事件。这意味着编写例行代码来创建参数并调用 ExecuteDataSet 或 ExecuteNonQuery,然后在每次更新后重新查询并重新绑定网格。

While there are lots of data binding methods out there (SQLcommandbuilder, objectcommandbuilder, etc) I have always had the best luck when I control more of the process. I have used this since the old DAO and ADO days. Call it old fashioned, but you will quickly develop a code base that is tested and easy to modify.

My current method is adapted for the ASP.NET gridview, but the principals are the same.

  1. Write data binding code that fills a dataset (ds.Tables(0))
  2. Build a grid with BoundColumns - don't let it "autogenerate columns".
  3. Add button columns for Edit, Update and Cancel. Show/hide columns as appropriate. One row in edit mode at a time.
  4. Bind the dataset to the grid's datasource. This isn't really creating a link back to the dataset, just populating a static grid from the dataset.
  5. Handle the Edit, Update and Cancel events. That means writing routine code that creates parameters and calls ExecuteDataSet or ExecuteNonQuery and then requeries and rebinds the grid after each update.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文