[英]C# Removing bindinglist item object on datagridview causes Exception
I am very new to C#
. 我是
C#
新手。
I need to be able to remove objects from my bindinglist
which is a data source for a datagridview
. 我需要能够从
bindinglist
删除对象,该列表是datagridview
的数据源。 When I remove the last item I get the following exception: 删除最后一个项目时,出现以下异常:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.DataGridViewRow.BuildInheritedRowStyle(Int32 rowIndex,
DataGridViewCellStyle inheritedRowStyle)
at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds,
Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect,
Rectangle clipRect, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds,
Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Here's My Code for removing the object: 这是删除对象的“我的代码”:
studies is the bindinglist of study objects. 研究是研究对象的绑定表。
private void removeComplete()
{
if (studies.Count == 0)
return;
// Create temp list of copleted studies
List<study> completedStudies = studies.Where(s => s.isComplete() == true).ToList();
if (studies.Count == 0)
{
// do nothing
}
else
{
// If I don't use this line, every row produces the same exception
studies.RaiseListChangedEvents = false;
foreach (study study in completedStudies)
{
try
{
studies.Remove(study);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
break;
}
// Turn it back on after turning it off above
studies.RaiseListChangedEvents = true;
// This is the point where it fails
studies.ResetBindings();
}
}
From what I can see, the datagridview still seems to be attempting to add a row which has just been removed from the source. 从我所看到的,datagridview似乎仍在尝试添加刚刚从源中删除的行。 Which is really odd to me.
这对我来说真的很奇怪。
Please help! 请帮忙!
Thanks to a comment on my original question and a bit more research, I have found it was because the removeComplete() method call needed to be on the UI thread. 感谢对我的原始问题的评论和更多的研究,我发现这是因为removeComplete()方法调用需要在UI线程上。 To do this I used BeginInvoke as shown below:
为此,我使用了BeginInvoke,如下所示:
public delegate void processDelegate();
private void processCompleted(object sender, EventArgs e)
{
processDelegate simpleDelegate = new processDelegate(removeComplete);
BeginInvoke(simpleDelegate);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.