Question

I am very new to C#.

I need to be able to remove objects from my bindinglist which is a data source for a 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. Which is really odd to me.

Please help!

Was it helpful?

Solution

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. To do this I used BeginInvoke as shown below:

public delegate void processDelegate();

private void processCompleted(object sender, EventArgs e)
{

    processDelegate simpleDelegate = new processDelegate(removeComplete);
    BeginInvoke(simpleDelegate);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top