Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8478801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:50:50+00:00 2026-06-10T18:50:50+00:00

I am very new to C# . I need to be able to remove

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:50:52+00:00Added an answer on June 10, 2026 at 6:50 pm

    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);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to JSP. I need to find in which file the
I'm very new at this, but I need to create new tables from existing
I'm very new to Symfony2 and I need to be able to test the
Very new to XSL (and XML for that matter), but I need to step
I am very new to SQL and i need to retreive a number of
I am very new (started today) to writing chrome extensions but need to write
I am VERY new to JSF, and I need some help doing, what I
I am very new to Python. I need to run diff.py(a python in built
I am very new to sql.The actual problem is much bigger. I need information
I have a very long string which includes many new lines ( it's a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.