如何从DataGridView中删除行? [英] How to remove rows from DataGridView?

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

问题描述

我有一个带有预加载DataGridView的Winform ...我想在选择或突出显示行并单击按钮时从datagridview中删除行...

I have a winform with preloaded DataGridView over it...I want to remove rows from datagridview on selecting or highlighting the rows and clicking over the button...

还想清除所有列....

Also want to clear all the columns....

当前我使用

foreach (DataGridViewRow dgvr in dataGridView2.Rows)
{
    if (dgvr.Selected == true)
    {
        dataGridView2.Rows.Remove(dgvr);
    }
}

但是它抛出了一个行或未提交"之类的异常....如果有人有更好的建议,这将是可理解的....

but it is throwing an exception that "rows or not commited" or something....it would be appreciable if any one have any better suggestions....

推荐答案

如果您在DataGridView上启用了 AllowUserToAddRows ,则可能是无意中删除了DataView底部的空白行.下一个用户创建的行的占位符.如果不需要,请尝试禁用此选项,否则请尝试使用如下代码:

If you have AllowUserToAddRows enabled on your DataGridView then you might be accidently deleting the empty row at the bottom of the DataView which is a placeholder for the next user created row. Try disabling this option if not required, otherwise try using code like this:

foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
    if(!row.IsNewRow)
       dataGridView1.Rows.Remove(row);
}

这篇关于如何从DataGridView中删除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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