DataGridView System.InvalidOperationException单元格不在DataGridView中 [英] DataGridView System.InvalidOperationException Cell is not in a DataGridView

查看:1420
本文介绍了DataGridView System.InvalidOperationException单元格不在DataGridView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次有一段时间,我可以在Google上找不到关于此异常的使用信息,希望有人可能会遇到这种情况。



我有一个DataGridView,我在Leave事件上验证空单元格,并删除这些行。



如果我将最后一个单元格留在最后一行,并从DGV中选取标签,则会抛出以下异常:


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


我没有使用数据绑定,如果我将断点放在 this.dataGridView1.Rows.RemoveAt(c.RowIndex); 它被命中,如果我进入这一行,在执行非用户代码期间抛出异常。



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



我的离开事件代码:

  private void dataGridView1_Leave(object sender,EventArgs e)
{

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

//验证空行
foreach(this.dataGridView1.Rows中的DataGridViewRow行)
{

foreach(Data.CridViewCell 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;
}

}
}


}
}
pre>

异常数据在这里:

  System.InvalidOperationException:单元格不在DataGridView中。单元格无法检索继承的单元格样式。 
在System.Windows.Forms.DataGridViewCell.GetInheritedStyle(DataGridViewCellStyle inheritedCellStyle,Int32 rowIndex,Boolean includeColors)
在System.Windows.Forms.DataGridView.OnCellValidating(DataGridViewCell& dataGridViewCell,Int32 columnIndex,Int32 rowIndex,DataGridViewDataErrorContexts上下文)
在System.Windows.Forms.DataGridView.CommitEdit(DataGridViewCell& dataGridViewCurrentCell,DataGridViewDataErrorContexts上下文,DataGridViewValidateCellInternal validateCell,Boolean fireCellLeave,Boolean fireCellEnter,Boolean fireRowLeave,Boolean fireRowEnter,Boolean fireLeave)
在System.Windows .Forms.DataGridView.EndEdit(DataGridViewDataErrorContextts context,DataGridViewValidateCellInternal validateCell,Boolean fireCellLeave,Boolean fireCellEnter,Boolean fireRowLeave,Boolean fireRowEnter,Boolean fireLeave,Boolean keepFocus,Boolean resetCurrentCell,Boolean resetAnchorCell)
在System.Windows.Forms.DataGridView。 ProcessDialogKe y(Key keyData)
在System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
在System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
在System.Windows。 Forms.Control.PreProcessMessage(Message& msg)
在System.Windows.Forms.Control.PreProcessControlMessageInternal(控制目标,消息和msg)
在System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& MSG)
在System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)
在System.Windows.Forms.Application.ThreadContext中的$ p
(IntPtr dwComponentID,Int32原因,Int32 pvLoopData) RunMessageLoop(Int32原因,ApplicationContext上下文)
在System.Windows.Forms.Application.Run(Form mainForm)


解决方案

如果您使用foreach-loop,则无法从收集的内容中删除项目G。在这里您要从 this.dataGridView1.Rows 中删除项目。



尝试这样:



(int i = 0; i< dataGridView1.Rows.Count; i ++)
{
DataGridViewRow row = dataGridView1.Rows [i];

  (int k = 0; k< row.Cells.Count; k ++)



$ b 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);

//减少我,因为集合变小
i--;
break;
}

}
}
}


For the first time in a while, I can find no use information on this exception on Google... hoping someone else may have come across it.

I have a DataGridView that I validate for empty cells on the Leave event and remove those rows.

If I leave the last cell empty in the last row and tab away from the DGV the following exception is thrown:

System.InvalidOperationException: Cell is not in a DataGridView. The cell cannot retrieve the inherited cell style.

I am not using Data Binding and a if I place a Breakpoint at this.dataGridView1.Rows.RemoveAt(c.RowIndex); It is hit and if if I step into this line the exception is thrown during the execution of non-user code...

I had thought this was to do with Uncommitted Changed but it seems explicitly committing the changes does not affect the outcome.

My Leave Event code:

    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;
                    }

                }
            }


        }
    }

Exception Data is here:

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)

解决方案

If you're using foreach-loop, you can't remove items from the collection it's using. Here you're removing items from this.dataGridView1.Rows.

Try this:

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;
          }

       }
    }
 }

这篇关于DataGridView System.InvalidOperationException单元格不在DataGridView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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