删除Gridview中的最后一个空行 [英] Remove last empty row in Gridview

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

问题描述

HI全部



我在winform中有Gridview,我想删除gridview中的最后一行我尝试使用下面的代码但它显示错误



我的代码

HI All

I have Gridview in winform, I want to remove last empty row in gridview I try to use below code but it show me error

My Code

bool Empty = true;

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                Empty = true;
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    if (dataGridView1.Rows[i].Cells[j].Value != null && dataGridView1.Rows[i].Cells[j].Value.ToString() != "")
                    {
                        Empty = false;
                        break;
                    }
                }
                if (Empty)
                {
                    dataGridView1.Rows.RemoveAt(i);
                }



显示错误



无法删除未提交的新行。



请帮帮我



谢谢


Showing Error

Uncommitted new row cannot be deleted.

Please help me

Thanks

推荐答案

for (int i = 1; i < dataGridView1.RowCount - 1; i++)
{
   Boolean isEmpty = true;
   for(int j=0;j<dataGridView1.Columns.Count; j++)
   {
     if (dataGridView1.Rows[i].Cells[j].Value.ToString() != "" )
      {
          isEmpty = false;
          break;
      }
    }
    if (isEmpty)
    {
       dataGridView1.Rows.RemoveAt(i);
       i--;
    }
 }


AllowUsersToAddRow 设为false。顺便说一下,你可能想要指定 DataGridView 而不是 GridView ,因为后者是web控件。
Set AllowUsersToAddRow as false. BTW, you might want to specify DataGridView instead of GridView since latter is web control.


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

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