如何清除有一个或多个空或空单元格的行? [英] How to remove row which has one or more empty or null cell?

查看:110
本文介绍了如何清除有一个或多个空或空单元格的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的winform上有datagridview。我在datagridview中显示记录。现在在datagridview上显示记录之后,我想从datagridview中删除一行或多个在该行的单元格中没有值的empy单元格的行。所以我正在检查每一行的每个单元格,如果有任何单元格为空或为空,那么我使用 RemoveAt()函数删除那些行。



我的代码是:

  for(int i = 0; i< dataGridView1.Rows。 Count = 1; i ++)
{
for(int j = 0; j< dataGridView1.Columns.Count; j ++)
{
if(string.IsNullOrEmpty(dataGridView1。行[i] .Cells [j] .Value.ToString()))
{
dataGridView1.Rows.RemoveAt(i);
break;
}
}
}

然后问题是它不正常工作,它不会删除所有具有空单元格的行。那么我应该怎么做?

解决方案

最简单的方式来做你想要的是循环遍历datagridview行。这样你的索引保持正确。

  for(int i = dataGridView1.Rows.Count -1; i> = 0; i--)
{
DataGridViewRow dataGridViewRow = dataGridView1.Rows [i];

foreach(dataGridViewRow.Cells中的DataGridViewCell单元格)
{
string val = cell.Value as string;
if(string.IsNullOrEmpty(val))
{
if(!dataGridViewRow.IsNewRow)
{
dataGridView1.Rows.Remove(dataGridViewRow);
break;
}
}
}
}

'做一些额外的事情,你可能不需要做(代码只是从我的测试应用程序剪切和粘贴)




  • 我通常把有问题的行抓到DataGridViewRow对象。

  • 我正在检查IsNewRow属性,因为我的网格是可编辑的。

  • 我将值分配给一个字符串变量(一个作为来转换它),因为你的方式是为我抛出一个异常。


I have datagridview on my winform. I am displaying records in the datagridview. Now after displaying the records on the datagridview, I want to remove the row from datagridview which has one or more empy cells that is no value in the cell for that row. So for that I am checking each cell for every row if there is any cell empty or null then I remove that rows using RemoveAt() function.

My code is :

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    if (string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[j].Value.ToString()))
                    {
                        dataGridView1.Rows.RemoveAt(i);
                        break;
                    }
                }
            }

Then problem is it does not work properly that it does not remove all the rows which has empty cell. So what should I do here ?

解决方案

The simplest way to do what you want is to loop through the datagridview rows in reverse. This way your indices stay correct.

for (int i = dataGridView1.Rows.Count -1; i >= 0; i--)
{
    DataGridViewRow dataGridViewRow = dataGridView1.Rows[i];

    foreach (DataGridViewCell cell in dataGridViewRow.Cells)
    {
        string val = cell.Value as string;
        if (string.IsNullOrEmpty(val))
        {
            if (!dataGridViewRow.IsNewRow)
            {
                dataGridView1.Rows.Remove(dataGridViewRow);
                break;
            }
        }            
    }
}

I'm doing a couple of extra things that you may not need to do (the code is just cut and pasted from my test application)

  • I usually grab the row in question into a DataGridViewRow object.
  • I am checking the IsNewRow property because my grid was editable.
  • I am assigning the value to a string variable (with an as to cast it) since the way you had it was throwing an exception for me.

这篇关于如何清除有一个或多个空或空单元格的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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