Thread: Exception "Object reference not set to an instance" iterating DataGrid

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    Exception "Object reference not set to an instance" iterating DataGrid

    Hi!

    i tried to iterate the DataGrid Control with below code:

    Code:
    String strSelDvdCat = cbDvdCat.SelectedItem.ToString();
                String strCat;
                int irowindex = 0;
    
    
                foreach (DataGridViewRow row in dgvViewDvds.Rows)
                {
                    try
                    {
                        DataGridViewCell cell = row.Cells[2];//Cat colum (2)
    
                        strCat = cell.Value.ToString();
    
                        //if (!(strSelDvdCat.ToLower().Contains(strCat.ToLower())))
                        {
                            dgvViewDvds.Rows.RemoveAt(irowindex);
                            MessageBox.Show(irowindex.ToString());
    
                        }
                        irowindex++;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
    The foreach() loops only about 4 times when i have 10 rows in the DataGrid. And then it gives the exception "Object reference not set to an instance", and quits the loop. Why does the loop stop prematurely?

    Thanks for any help.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    foreach (DataGridViewRow row in dgvViewDvds.Rows)
                {
    ...
                       dgvViewDvds.Rows.RemoveAt(irowindex);
    ...
    You can't modify a foreach enumerator inside the loop. It's not allowed. You'll have to do something like creating a list of rows to remove, and then remove all of the rows in that list after the foreach loop.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    Thanks. Yes my bad. i just used the hide() function to hide the rows instead of trying delete 'em.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-19-2011, 03:12 AM
  2. struct instance "undeclared" (first use in this function)
    By SamuraiDave in forum C Programming
    Replies: 2
    Last Post: 09-14-2005, 03:21 AM
  3. ERRPR: Object reference not set to an instance of an object
    By blackhack in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2005, 05:27 PM
  4. Replies: 1
    Last Post: 06-21-2005, 08:10 AM
  5. "Object reference not set to an instance of an object"
    By Manitoadlet in forum C++ Programming
    Replies: 6
    Last Post: 09-10-2002, 06:09 PM