0
저는 매우 신입생 C#
입니다.C# DataGridview에서 바인딩 목록 항목 객체를 제거하면 예외가 발생합니다.
datagridview
의 데이터 소스 인 내 bindinglist
에서 개체를 제거 할 수 있어야합니다. 내가 마지막 항목을 제거하면 나는 다음과 같은 예외가 :
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)
여기 객체를 제거 내 코드입니다 :
연구는 연구 대상의는 바인딩이다.
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();
}
}
내가 볼 수 있듯이, datagridview는 여전히 소스에서 제거 된 행을 추가하려고 시도하는 것 같습니다. 어느 것이 나에게 정말로 이상합니다.
도와주세요!
이 호출을 그래픽 스레드에 동기화 했습니까? – Seb
나는 그렇게 생각하지 않는다. 내가 말했듯이 저는 C#을 처음 접했습니다. 게시 된 코드는 양식 클래스에 있으며 조사 객체의 'completed'이벤트에서 호출됩니다. – user1094128