有一段時間以來,我第一次在Google上找不到有關此異常的任何使用信息......希望其他人可能遇到過它。DataGridView System.InvalidOperationException單元格不在DataGridView中
我有一個DataGridView,我驗證Leave事件上的空單元格,並刪除這些行。
如果我從DGV離開最後一個單元格在最後一排空和標籤客場以下異常被拋出:
System.InvalidOperationException:小區不在一個DataGridView。 單元格無法檢索繼承的單元格樣式。
我不使用數據綁定和,如果我把一個斷點在this.dataGridView1.Rows.RemoveAt(c.RowIndex);
被擊中,如果,如果我踏入這行的異常的非用戶代碼的執行過程中引發...
我原以爲這是和Uncommitted Changed一起做的,但似乎明確地提交了這些更改並不影響結果。
我Leave
事件代碼:
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;
}
}
}
}
}
異常數據是在這裏:
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)
有什麼價值財產AllowUserToAddRows設置? – gulshanm01