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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1560559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:52:55  来源:igfitidea点击:

C# - how do I refresh DataGridView after removing rows

c#winformsdatagridviewrefreshpopulate

提问by sa125

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:

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

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?

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

采纳答案by Anand Shah

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.

如果你已经将你的数据网格绑定到一个 Observable 集合(如果没有,那么你应该)那么你需要实现 INotifyCollectionChanged 接口,以便监听器收到动态变化的通知,例如当项目被添加和删除或整个列表被刷新。

HTH

HTH

回答by Hyman Marchetti

Don't you need to rebind the data grid?

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

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

?

?

回答by Austin Salonen

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

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

回答by Raúl Roa

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

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

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.

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

回答by dell

Try removing the actual items from your binding source instead.

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

回答by Sam Casil

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

如果我理解正确,您想从 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)

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

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

  3. 请注意,此时,虽然该项目已从 DGV 中删除,但它仍未从 Access DB 中删除。您需要在 DataSet/DataTable 上调用 TableAdapter Update 方法以将删除提交到数据库,例如 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.

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

回答by TK Mphahlele

this code could be useful:

此代码可能有用:

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

Hope this helps.

希望这可以帮助。