Like Tree1Likes
  • 1 Post By itsme86

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

This is a discussion on Exception "Object reference not set to an instance" iterating DataGrid within the C# Programming forums, part of the General Programming Boards category; Hi! i tried to iterate the DataGrid Control with below code: Code: String strSelDvdCat = cbDvdCat.SelectedItem.ToString(); String strCat; int irowindex ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    363

    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.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    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.
    geek@02 likes this.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    363
    Thanks. Yes my bad. i just used the hide() function to hide the rows instead of trying delete 'em.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21