删除最后一行dataGridView的最后一行 [英] Removing last empty row of dataGridView

查看:330
本文介绍了删除最后一行dataGridView的最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些空的datagridview使用数据源是List通过BindingSource。因为AllowUserToAddRows = true,最初datagridview有一个空行。当我选择这个行并关闭窗体时,datagridview创建新的空对象。为什么这样做?如何防止这种情况?
谢谢。



选择空行:

我得到这个:



结论:经过多次尝试找到解决方案,我决定使用datagridview和两个按钮创建UI控件(添加行和删除行)。在这一点上,我停止了按计划进行的尝试,但我仍然很高兴知道解决方案。

解决方案

你可以处理 RowValidating 事件以验证行是否为空。如果是,则取消事件和行将不被添加。

  if(gridView [0,e.RowIndex] .Value = = null)
e.Cancel = true;


I have some empty datagridview which uses data source is List through BindingSource. Because AllowUserToAddRows = true, initially the datagridview has one empty row. When I just select this row and close the form then datagridview creates new empty object. Why it does it? How to prevent this? Thanks.

Selecting of empty row: And I get this:

Conclusion: After many attempts to find a solution I decided to create UI control with datagridview and two buttons (add row and delete row). At this point I stopped trying to do as planned, but I'd still be pleased to know solutions.

解决方案

You can handle RowValidating event to verify if row is empty. If so, then cancel event and row will not be added.

if (gridView[0, e.RowIndex].Value == null)
    e.Cancel = true;

这篇关于删除最后一行dataGridView的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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