Click here to Skip to main content
15,668,691 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I work on a c# quiz. So the way it's going to work is that when someone clicks on the Datagrid view that shows the question. Whichever question id is clicked it will show the matching multiple choice question.

This is my code that shows the data from the grid into the text box

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            if (e.RowIndex >= -1)
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                textBox2.Text = row.Cells[0].Value.ToString();
                textBox3.Text = row.Cells[1].Value.ToString();
                textBox4.Text = row.Cells[2].Value.ToString();
                textBox5.Text = row.Cells[3].Value.ToString();

I'm getting an Index is out of range error. I'm not sure what the issue is. I'm just trying to show the data in the other four rows of my access database.

What I have tried:

I have tried removing the index but it not working. I saw another post with this error but it's not really working.
Updated 4-Nov-21 6:58am
Richard MacCutchan 4-Nov-21 12:57pm    
You cannot have a RowIndex of -1. Array/List indices go from zero to one less than the number of entries.
Mahdi Dahir 4-Nov-21 13:03pm    
Hi, I did that because the Datagridview was only showing the data in the second row since the computer read the first row as 0. Even though I'm still getting an error.
Richard MacCutchan 4-Nov-21 13:06pm    
You need to provide more detailed information. What is the actual value of e.RowIndex when the error occurs?
Mahdi Dahir 4-Nov-21 13:07pm    
I'm getting the same error when the value is 0 or -1.
Richard MacCutchan 4-Nov-21 13:28pm    
Firstly, if the value is -1 then you have not clicked in a valid row and you should ignore the event. If the value is zero and you still get the error then there is something wrong with your datagridview. You need to use your debugger to find out exactly what item is causing the error.

1 solution

You can try to change
if (e.RowIndex >= -1)
if ((e.RowIndex > -1) && (e.RowIndex < dataGridView1.Rows.Count))
to filter out that edge case where you get an array-out-of-bounds issue.
Share this answer
Mahdi Dahir 4-Nov-21 13:12pm    
Hi, I'm this didn't help my issue. I'm pretty new to c# so I'm not sure what I'm looking for. Sorry for being such a noob.
phil.o 4-Nov-21 13:30pm    
Then it is time for you to discover a whole new field: debugging :)
The error you get need to be investigated, and for that you need to compile the project and lauch a debug session (F5 in Visual Studio).
Tutorial: Learn to debug C# code using Visual Studio[^]
Mahdi Dahir 4-Nov-21 14:40pm    
Hi, so part is giving me an error: textBox3.Text = row.Cells[1].Value.ToString();

is showing an error. However, if I remove that piece of code it just shows the error in the next line of code.
phil.o 4-Nov-21 14:44pm    
As I told you: at this point, you are the only one who can find out the issue, because you are the only one having all required elements. And I gave you the way for that: the debugger. Did you follow the link I provided?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900