DataGridView.Rows.RemoveAt(int index)不删除Windows窗体中的行? [英] DataGridView.Rows.RemoveAt(int index) doesn't remove row in windows form?

查看:1003
本文介绍了DataGridView.Rows.RemoveAt(int index)不删除Windows窗体中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int i = dataGridView1.CurrentRow.Index; 
dataGridView1.Rows.RemoveAt(i);

上述删除当前所选行的代码(只允许选择1行),但我仍然可以看到该行仍然在datagridview中。我是否想念更新或无效?



ps:dataGridView1是数据绑定的。



ps2:我使用linq查询绑定数据,我不想再次查询只是为了显示一行被删除,我认为它会降低性能。

解决方案

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


  1. 使用您的DGV的DataGridViewRowCollection,而不是DataTow的DataRowCollection。 DataGridViewRow具有指定是否选择行的Selected属性。


  2. 确定要删除一行后,可以使用删除DataGridViewRowCollection的方法,从网格中删除项目,例如请注意,在这一点上,尽管该项目已从DGV中删除,但仍未从Access中删除该项目。 DB。您需要调用DataSet / DataTable上的TableAdapter Update方法将删除提交给DB,例如YerTableAdapter.Update(YerDataSet)


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


int i = dataGridView1.CurrentRow.Index;
dataGridView1.Rows.RemoveAt(i);

The above code for deleting current selected row (only allow to select 1 row) but I still can see the row remains in datagridview. Did I miss an update or invalidate ?

ps: dataGridView1 is data-bound.

ps2: I use linq query to bind data and I don't want to query again for just to show a row was deleted, I think it will slow down performance.

解决方案

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.

这篇关于DataGridView.Rows.RemoveAt(int index)不删除Windows窗体中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆