如何删除用户取消添加过程时要添加的DataGridView行? [英] How to delete a DataGridView row that is being added when the user cancels the addition process?

查看:95
本文介绍了如何删除用户取消添加过程时要添加的DataGridView行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据绑定 DataGridView 。当添加新行并且用户按 Esc 时,我想删除整行。如何执行此操作?

I have a databound DataGridView. When a new row is added and the user presses Esc I want to delete the entire row. How can I do this?

推荐答案

如果要从DataGrid中删除行,您必须使用BindingSource而不是列表,否则,在执行此操作时您将收到异常。

If you want to remove rows from the DataGrid, you have to use a BindingSource instead of a list, otherwise you will get an exception when doing it.

尝试此操作:

public partial class YourForm : Form {

  private BindingSource _source = new BindingSource();

  public YourForm() {
    List<Model> list = _service.GetList();
    _source.DataSource = list;
    _grid.DataSource = _source;
  }
}

现在你可以玩数据源和网格将表现自己。每次更改后,别忘记调用_grid.Refresh()。

Now you can play around with your datasource and the grid will behave itself. Don't forget to call _grid.Refresh() after each change.

干杯,

Andre carlucci

Andre carlucci

这篇关于如何删除用户取消添加过程时要添加的DataGridView行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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