C# 编辑sql表数据而不丢失现有数据

发布于 2024-11-29 23:25:55 字数 592 浏览 0 评论 0原文

可能的重复:
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 技术交流群。

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

发布评论

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

评论(2

眼泪也成诗 2024-12-06 23:25:55

我能想到的更简单的方法是,在填充网格时,我会看到我从数据库添加了多少现有行。我会留意它。然后,当用户单击“保存”时,将从该计数之后的行更新数据库。更新数据库后,我会将计数更新为新行数。
即使您不想允许用户编辑,同样的情况也适用,您可以编写事件来检查可编辑行是否小于 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.

別甾虛僞 2024-12-06 23:25:55

作为替代方案,您可以为用户提供一个新表单,以便他们像许多新表单一样添加新记录。我喜欢用户能够向 DataGridView 添加行的想法,但最终放弃了与之抗争。与 DGV 中的行相比,您在表单中的控制要容易得多。

回答你的问题 - 与其与来源作斗争,不如请 DataGridView 主持:(

dataGridView1.DataSource = bs;
dataGridView1.ReadOnly = true;
dataGridView1.CurrentCellChanged += delegate 
{
    DataGridViewRow row = dataGridView1.CurrentRow;
    bool readOnly = row == null ||
        row.Index != dataGridView1.NewRowIndex;
    dataGridView1.ReadOnly = readOnly;
};

并且不要在列表上设置允许编辑)

来源: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:

dataGridView1.DataSource = bs;
dataGridView1.ReadOnly = true;
dataGridView1.CurrentCellChanged += delegate 
{
    DataGridViewRow row = dataGridView1.CurrentRow;
    bool readOnly = row == null ||
        row.Index != dataGridView1.NewRowIndex;
    dataGridView1.ReadOnly = readOnly;
};

(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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文