7

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

private void removeRows(DataGridView dgv) {

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

}

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

4

7 回答 7

3

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

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

?

于 2009-10-13T14:24:38.517 回答
3

有时刷新数据网格视图是不够的,它的包含父视图也应该刷新。

试试这个:

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

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

于 2009-10-13T14:34:26.250 回答
2

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

高温高压

于 2009-10-13T14:30:28.727 回答
2

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

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

于 2011-09-22T02:42:09.517 回答
2

此代码可能很有用:

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

希望这可以帮助。

于 2015-04-05T02:06:25.480 回答
1

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

于 2009-10-13T14:28:03.733 回答
0

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

于 2009-10-13T14:47:08.273 回答