C# - 删除行后如何刷新 DataGridView

发布于 2024-08-07 19:42:06 字数 361 浏览 6 评论 0原文

在我的代码中,我需要在重复间隔后从 DataGridView 中删除行,因此当计时器到期时我调用以下函数:

private void removeRows(DataGridView dgv) {

    foreach (DataGridViewRow row in dgv.Rows)
    {
        // if some condition holds
        dgv.Remove(row);                
    }
    dgv.Refresh();

}

我知道这些行已成功从 DataGridView 中删除,但无论出于何种原因它们仍然保留在显示中。关于我可能做错了什么有什么建议吗?

In my code I need to remove rows from the DataGridView after a recurring interval, and so I call the following function when a timer expires:

private void removeRows(DataGridView dgv) {

    foreach (DataGridViewRow row in dgv.Rows)
    {
        // if some condition holds
        dgv.Remove(row);                
    }
    dgv.Refresh();

}

I know the rows are successfully deleted from the DataGridView, though they still remains in the display for whatever reason. Any tips on what I might be doing wrong?

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

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

发布评论

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

评论(8

瑾兮 2024-08-14 19:42:06

不需要重新绑定数据网格吗?

dgrv.Datasource = [whatever data source];
dgrv.DataBind();

Don't you need to rebind the data grid?

dgrv.Datasource = [whatever data source];
dgrv.DataBind();

?

许你一世情深 2024-08-14 19:42:06

有时刷新数据 gridview 是不够的,它的包含父级也应该刷新。

试试这个:

dgv.Refresh(); // Make sure this comes first
dgv.Parent.Refresh(); // Make sure this comes second

您还可以编辑源并将新数据源附加到控件。

Sometimes refreshing the data gridview is not enough and its containing parent should be refreshed too.

Try this:

dgv.Refresh(); // Make sure this comes first
dgv.Parent.Refresh(); // Make sure this comes second

You could also edit your source and attach the new datasource to the control.

萌酱 2024-08-14 19:42:06

这段代码可能有用:

dataGridView.DataSource = null;
dataGridView.Update();
dataGridView.Refresh();
dataGridView.DataSource = SomeDataSource;

希望这有帮助。

this code could be useful:

dataGridView.DataSource = null;
dataGridView.Update();
dataGridView.Refresh();
dataGridView.DataSource = SomeDataSource;

Hope this helps.

牵你手 2024-08-14 19:42:06

如果您已将数据网格绑定到可观察集合(如果没有,那么您应该这样做),那么您将需要实现 INotifyCollectionChanged 接口,以便侦听器收到动态更改的通知,例如添加和删除项目或刷新整个列表时。

华泰

If you have bound your datagrid to an Observable Collection (if not then you should) then you will need to implement INotifyCollectionChanged interface so that listeners are notified of dynamic changes, such as when items get added and removed or the whole list is refreshed.

HTH

So要识趣 2024-08-14 19:42:06

如果我理解正确的话,您想要删除用户从 DGV 中选择的行。

  1. 使用 DGV 的 DataGridViewRowCollection 而不是 DataTable 的 DataRowCollection。 DataGridViewRow 具有 Selected 属性,该属性指示是否选择了行。

  2. 一旦确定要删除某行,就可以使用 DataGridViewRowCollection 的 Remove 方法从网格中删除该项目,例如 YerDataGridView.Rows.Remove(row)

  3. 注意,此时,虽然该项目已从DGV中删除,它仍然没有从 Access DB 中删除。您需要在 DataSet/DataTable 上调用 TableAdapter Update 方法将删除提交到 DB,例如 YerTableAdapter.Update(YerDataSet)

我通常会在删除所有要从数据库中删除的项目后调用一次 Update 来提交更改DGV。

If I understand you correctly, you want to delete rows selected by a user from your DGV.

  1. Use the DataGridViewRowCollection of your DGV rather than the DataRowCollection of the DataTable. The DataGridViewRow has the Selected property that indicates whether a row is selected or otherwise.

  2. Once you have determined that a row is to be deleted, you can use the Remove method of the DataGridViewRowCollection to delete the item from the grid, e.g. YerDataGridView.Rows.Remove(row)

  3. Note that at this point, although the item is removed from the DGV, it still has not been deleted from the Access DB. You need to call the TableAdapter Update method on your DataSet/DataTable to commit the deletions to the DB, e.g. YerTableAdapter.Update(YerDataSet)

I normally would call Update once to commit the changes only after having removed all the items to be deleted from the DGV.

苍风燃霜 2024-08-14 19:42:06

如果它是数据绑定网格,您应该处理绑定源本身而不是网格。

If it's a data-bound grid, you should be working on the binding source itself instead of the grid.

紧拥背影 2024-08-14 19:42:06

尝试从绑定源中删除实际项目。

Try removing the actual items from your binding source instead.

她如夕阳 2024-08-14 19:42:06

我遇到了同样的问题,我已经找到了根本原因。
您所要做的就是在重新加载之前初始化数据库上下文对象并查看魔法。

I had same problem and I have find out the root cause.
all you have to do initialize you database context object in before reload and see the magic .

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