简体   繁体   English

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

[英]C# - how do I refresh DataGridView after removing rows

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 中删除行,因此当计时器到期时我调用以下 function:

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.我知道这些行已从 DataGridView 中成功删除,但无论出于何种原因它们仍保留在显示中。 Any tips on what I might be doing wrong?关于我可能做错了什么的任何提示?

Don't you need to rebind the data grid? 你不需要重新绑定数据网格吗?

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

?

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. 您还可以编辑源并将新数据源附加到控件。

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. 如果已将datagrid绑定到Observable Collection(如果不是,则应该),那么您将需要实现INotifyCollectionChanged接口,以便向侦听器通知动态更改,例如何时添加和删除项目或刷新整个列表。

HTH HTH

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. 使用DGV的DataGridViewRowCollection而不是DataTable的DataRowCollection。 The DataGridViewRow has the Selected property that indicates whether a row is selected or otherwise. DataGridViewRow具有Selected属性,该属性指示是否选择了行。

  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, eg YerDataGridView.Rows.Remove(row) 一旦确定要删除一行,就可以使用DataGridViewRowCollection的Remove方法从网格中删除该项,例如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. 请注意,此时,虽然该项目已从DGV中删除,但它仍未从Access DB中删除。 You need to call the TableAdapter Update method on your DataSet/DataTable to commit the deletions to the DB, eg YerTableAdapter.Update(YerDataSet) 您需要在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一次提交更改。

如果它是一个数据绑定网格,您应该使用绑定源本身而不是网格。

this code could be useful: 这段代码可能很有用:

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

Hope this helps. 希望这可以帮助。

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

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.您所要做的就是在重新加载之前初始化数据库上下文 object 并查看魔术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM