"System.NullReferenceException was unhandled" error after last valid row.

Hi, I'm using Visual Studio 2010 C#. I have a DataGridView on one of my Forms that is connected to SQL database table. That part works. I'm also using the dataGridView1_RowEnter Event to populate the selected information into different Textboxes as I scroll down the grid. That works until I get past the last row.

As soon as I go beyond the length of the table (past the last row) and enter the first NULL row, I get the following error.  

ERROR: >> System.NullReferenceException was unhandled

Why is the  "if (e.RowIndex >= 0)" logic statement not preventing the error and how do I solve this problem?

private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
            DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
            if (e.RowIndex >= 0)
                textBox1.Text = row.Cells[1].Value.ToString();
                textBox2.Text = row.Cells[2].Value.ToString();
                textBox3.Text = row.Cells[3].Value.ToString();
                textBox4.Text = row.Cells[4].Value.ToString();

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
>>Why is the  "if (e.RowIndex >= 0)" logic statement not preventing the error and how do I solve this problem?

What is the value of e.RowIndex when it fails.  You are apparently assuming it is less than zero.
AndyAinscowFreelance programmer / ConsultantCommented:
I suspect you want something like:

if (!row.IsNewRow)   //if (e.RowIndex >= 0)


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
This statement needs to be in the if statement, first statement inside if statement.

 DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

AndyAinscowFreelance programmer / ConsultantCommented:
Why does that assignment need to be inside the if block?
Mr_FulanoAuthor Commented:
Hi Fernando, I had it that way and retried your suggestion just to make sure, but that still fails.
Mr_FulanoAuthor Commented:
Hi Andy, yes...your suggestion works. The last row is negative and keeps increasing each time I enter it (i.e. -1. -2. -3...etc).

Is there a way to not show that last row at all?
Fernando SotoRetiredCommented:
Hi Andy, I was thinking that if this was happening when he passed the last row it had a -1 as the index and therefore would be an out of bounds index or no row. Was not near a computer to test and wanted him to have something to test for the moment.
Mr_FulanoAuthor Commented:
Thank you both...!!!
AndyAinscowFreelance programmer / ConsultantCommented:
Hi Fernando, I could understand why you suggested that for some of the other possible events but was puzzled why for the RowEnter event.  Thanks for clarifying.
Fernando SotoRetiredCommented:
Not a problem Andy, have a great day.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.