隐藏除 DataGridView 中正在编辑的行之外的所有行

发布于 2024-12-10 12:48:59 字数 455 浏览 0 评论 0原文

我想减少用户编辑错误记录的可能性,因此一旦他们开始编辑 datagridview 中的一行,我想隐藏所有行。

我尝试过:

foreach (DataGridViewRow dgvr in rdGridView.Rows)
{
    if (dgvr.Index != e.RowIndex )
    {
        dgvr.DefaultCellStyle.BackColor = Color.DarkGray;
        dgvr.ReadOnly = true
    }
}

但这并没有提供太多保护。

我可以更新 DataView 以仅显示该行,并将 dataview 再次分配给 datgridview,但这会将焦点移出单元格,并且单元格不会进入编辑模式。

在像上面这样的 foreach 循环中执行 dgvr.Visible = false 也会给出异常

I want to reduce the probability of users editing the wrong record so once they start editing one row in datagridview i want to hide all rows.

I tried this:

foreach (DataGridViewRow dgvr in rdGridView.Rows)
{
    if (dgvr.Index != e.RowIndex )
    {
        dgvr.DefaultCellStyle.BackColor = Color.DarkGray;
        dgvr.ReadOnly = true
    }
}

But this doesn't give much of a protection.

I can update the DataView to only show that row and assign the dataview again to the datgridview but that would take the focus out of the cell, and cell doesn't go into edit mode.

Doing dgvr.Visible = false in a foreach loop like above also gives exception

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

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

发布评论

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

评论(3

筱果果 2024-12-17 12:48:59

也许您应该尝试在 DataGridView 中加载数据,但使用字段从要编辑的行加载数据。这样,您可以将 DataGridView 设置为 ReadOnly,然后单击时,可以将数据从可编辑字段复制到文本框,从而允许用户编辑和保存。

Perhaps you should try loading your data in the DataGridView, but using fields to load the data from the row you want to edit. This way, you can set the DataGridView to ReadOnly, and on click, you can copy the data from the editable fields to textboxes, allowing the user to edit and save.

木有鱼丸 2024-12-17 12:48:59

我会寻求更多不同的解决方案。双击一行(无论是哪个单元格,如果您打算编辑一行的整个单元格),您将打开一个新的表单(小窗口),其中包含文本框和一个“保存”按钮。文本框的数量取决于要编辑的列数。
因此,将 DGV 中的所有数据传递给这个新表单的构造函数,并将数据传递给相应的文本框。

这样您就可以编辑与 DGV 分离的所有数据。顺便说一句,当显示新表单时,使用 ShowDialog() 方法将创建此新表单,该方法只能编辑(在关闭它之前,用户将无法执行任何其他操作,只能编辑此表单上的数据)。

I would go for a bit more different solution. On a double click on a row (no matter which cell if you intend to edit whole cells of a row) you open a new Form (small window) which will have textBoxes inside and a button Save. The number of textBoxes depends on the number of columns to edit.
So pass all the data from DGV to the constructor of this new form and pass the data to according textBoxes.

So this way you can edit all the data seperated from the DGV. And btw, when showing new Form, use ShowDialog() method which will create this new form editable only (before closing it, user will not be able to do anyting else but edit data on this form).

习惯成性 2024-12-17 12:48:59

找不到比将所有行设为只读并将其变为灰色(正在编辑的行除外)更好的方法了。

Couldn't find anything better than making all the rows read only and turning them gray except the one being edited.

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