首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataGridView System.InvalidOperationException单元不在DataGridView中

DataGridView System.InvalidOperationException单元不在DataGridView中
EN

Stack Overflow用户
提问于 2014-05-22 05:57:02
回答 1查看 1.9K关注 0票数 0

这是一段时间以来我第一次在Google上找到关于这个异常的没有用的信息.希望其他人可能会发现。

我有一个DataGridView,用于验证左事件上的空单元格并删除这些行。

如果我将最后一个单元格空在DGV的最后一行和选项卡中,将引发以下异常:

System.InvalidOperationException:单元格不在DataGridView中。单元格无法检索继承的单元格样式。

我不使用数据绑定,如果我在this.dataGridView1.Rows.RemoveAt(c.RowIndex);放置一个断点,它就会被击中,如果我进入这一行,异常将在非用户代码的执行过程中抛出.

我原以为这与未提交的更改有关,但似乎明确提交更改并不影响结果。

我的Leave事件代码:

代码语言:javascript
复制
    private void dataGridView1_Leave(object sender, EventArgs e)
    {

        if (this.dataGridView1.IsCurrentRowDirty || this.dataGridView1.IsCurrentCellDirty) this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

        //Validate for empty rows
        foreach(DataGridViewRow row in this.dataGridView1.Rows)
        {

            foreach(DataGridViewCell c in row.Cells)
            {

                if(c.Value == null || c.Value.ToString() == String.Empty)
                {
                    if (c.EditedFormattedValue == null || c.EditedFormattedValue.ToString() == "")
                    {
                        this.dataGridView1.Rows.RemoveAt(c.RowIndex);
                        break;
                    }

                }
            }


        }
    }

异常数据在这里:

代码语言:javascript
复制
System.InvalidOperationException: Cell is not in a DataGridView. The cell cannot retrieve the inherited cell style.
   at System.Windows.Forms.DataGridViewCell.GetInheritedStyle(DataGridViewCellStyle inheritedCellStyle, Int32 rowIndex, Boolean includeColors)
   at System.Windows.Forms.DataGridView.OnCellValidating(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, DataGridViewDataErrorContexts context)
   at System.Windows.Forms.DataGridView.CommitEdit(DataGridViewCell& dataGridViewCurrentCell, DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave)
   at System.Windows.Forms.DataGridView.EndEdit(DataGridViewDataErrorContexts context, DataGridViewValidateCellInternal validateCell, Boolean fireCellLeave, Boolean fireCellEnter, Boolean fireRowLeave, Boolean fireRowEnter, Boolean fireLeave, Boolean keepFocus, Boolean resetCurrentCell, Boolean resetAnchorCell)
   at System.Windows.Forms.DataGridView.ProcessDialogKey(Keys keyData)
   at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
   at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
   at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
   at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
   at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
   at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-22 07:05:11

如果使用foreach-循环,则不能从它使用的集合中删除项。在这里,您将从this.dataGridView1.Rows.中删除项

试试这个:

代码语言:javascript
复制
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
   DataGridViewRow row = dataGridView1.Rows[i];

   for (int k = 0; k < row.Cells.Count; k++)
   {
      DataGridViewCell c = row.Cells[k];

      if (c.Value == null || c.Value.ToString() == String.Empty)
      {
         if (c.EditedFormattedValue == null || c.EditedFormattedValue.ToString() == "")
         {
            this.dataGridView1.Rows.RemoveAt(c.RowIndex);

            // Decrease i, as the collection got smaller
            i--;
            break;
          }

       }
    }
 }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23798825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档