C# 编辑sql表数据而不丢失现有数据
可能的重复:
C# WinForms BindingList & DataGridView - 不允许编辑会阻止创建新行?我该如何解决这个问题?
我有一个sql数据表,我不希望用户删除或修改任何内容,他们只能根据需要添加新行。
我的解决方案是使用 datagridview 显示现有数据,但我被卡住了,因为我不能在不拒绝添加新行的情况下拒绝编辑或删除,所以这不是一个好方法。否则,使用 update 方法将覆盖用户已更改的所有数据(稍后我可能可以为此授予覆盖权限)。
我可以制作一个只读的 dgw 来显示现有数据,并允许用户使用文本框逐一输入新数据,但这是一项更大的工作,而且可能没有必要。
有没有一种简单的方法可以允许添加新行而不编辑现有数据?也许操纵数据表?
谢谢。
Possible Duplicate:
C# WinForms BindingList & DataGridView - disallowing EDIT prevents creation of a NEW row? How can I address this?
I have a sql datatable, and I don't want the users to delete or modify anything, they can only add new rows if they want.
My solution was to use datagridview to show the existing data, but I'm stuck, because I cannot deny editing or deleting without denying adding a new row, so this is not a good way. Otherwise using update method will override all the data the user has changed (later on maybe I can give overriding permissions for that).
I could make a read-only dgw to show the existing data, and allow the user to enter the new data one by one with textboxes, but that's a bigger work and maybe unnecessary.
Is there a simple way to allow adding new row without letting editing the existing data? Maybe manipulating the datatable?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能想到的更简单的方法是,在填充网格时,我会看到我从数据库添加了多少现有行。我会留意它。然后,当用户单击“保存”时,将从该计数之后的行更新数据库。更新数据库后,我会将计数更新为新行数。
即使您不想允许用户编辑,同样的情况也适用,您可以编写事件来检查可编辑行是否小于 tis 计数,然后禁止他编辑。
The simpler way i could think of is, while populating the grid, ill see how many existing rows i am adding from DB. Ill keep a count on it. Then when user clicks save, ill update the DB from rows after this count. Once update the DB, ill update the count to new rows count.
The same holds good even if you dont want to allow the user to edit, you can write events where you can check if the editable row is less than tis count, then disallow him to edit.
作为替代方案,您可以为用户提供一个新表单,以便他们像许多新表单一样添加新记录。我喜欢用户能够向 DataGridView 添加行的想法,但最终放弃了与之抗争。与 DGV 中的行相比,您在表单中的控制要容易得多。
回答你的问题 - 与其与来源作斗争,不如请 DataGridView 主持:(
并且不要在列表上设置允许编辑)
来源:C# WinForms BindingList & DataGridView - 不允许编辑会阻止创建新行?我该如何解决这个问题?
As an alternative you could provide the user with a new Form in order for them to add a new record like a lot of new forms. I liked the idea of a user being able to add a row to a DataGridView but in the end gave up fighting with it. You just have far easier control in a Form as opposed to a row in a DGV.
In answer to your question - Rather than fight the source, perhaps ask the DataGridView to officiate:
(and don't set AllowEdit on the list)
Source: C# WinForms BindingList & DataGridView - disallowing EDIT prevents creation of a NEW row? How can I address this?