如何解决以下文本中的错误。 [英] How to solve error under the following text .

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

问题描述

除非datagridview数据绑定到支持更改通知并允许删除的IBindingList,否则无法以编程方式删除行。



复制的其他信息来自下面的非解决方案

"Rows cannot be programmatically removed unless the datagridview is data-bound to an IBindingList that support change notification and allows deletion."

additional information copied from non-solution below

// Fill Data in grid table
private void btnAddRow_Click(object sender, EventArgs e)
{
   if (grdUserlist.Rows.Count > 0)
   {
      if (lstUserList.Count > 0)
      {
         foreach (var item in  userbol.UserListByGroupIDForMessage())
         {
            lstUserList.Add(item);
         }
      }
      else
      {
         lstUserList = null;
         lstUserList = userbol.UserListByGroupIDForMessage();
      }

      if (lstUserList.Count > 0)
      {
         grdUserlist.DataSource = null;
         grdUserlist.DataSource = lstUserList;
      }
   }
}

// Delete rows function
private void btnDelete_Click(object sender, EventArgs e)
{
   for (int i = 0; i < grdUserlist.Rows.Count;i++ )
   {
      DataGridViewRow delrow=grdUserlist.Rows[i];
      if(delrow.Selected==true)
      {
         grdUserlist.Rows.RemoveAt(i);
      }
   }
}

推荐答案

查看类似帖子:





无法以编程方式删除行 [ ^ ]
check this similar post:


Rows cannot be programmatically removed[^]


你不能使用

you can't use
grdUserlist.Rows.RemoveAt(i);





因为我认为你在使用 IBindingList

如果您在程序上添加代码如





as i think you are using IBindingList.
if you add the code programtically like

datagridview1.Rows.Add();





然后就可以了。



Then it would have worked.


试试这个:

Try this:
private void btnDelete_Click(object sender, EventArgs e)
{
    if (!(e.RowIndex < 0 || e.ColumnIndex < 0))
    {
        DataGridViewButtonCell cell = grdUserlist.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewButtonCell;
        if (cell != null)
            grdUserlist.Rows.RemoveAt(cell.RowIndex);
    }
}


这篇关于如何解决以下文本中的错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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