Click here to Skip to main content
15,797,576 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
If i could have some guidance please.

C# winform

form has 24 picturboxes on it, datagridview has the information to load images.

I do realize why this error is happening but im not sure if using the try and catch is the best way around my problem

24 pictureboxes and only 16 records in my datagridview and i get the error, obviously.

What I have tried:

I have tried below and it catches the index error and i have my code working but still not sure if this is the best way around it.
        DataGridView.Rows[16].Selected = true;

DataGridView.Rows[16].Selected = true;
Updated 30-Apr-23 22:03pm
Member 15627495 1-May-23 2:45am    
Hello amthp !

It's hard to guess this way.. provide more code lines please.

check your 25 pictures paths, and check the var with all datas in, maybe a 'empty' field is in.

try/catch/finally :
It's often used for 'debug' to have a feedback about errors in code.
but 'debug' is not about ""try/catch/finally".

beginers codes go through 'try/catch/finally' because it's easy and don't crash the App.
it's a misuse.
this kind of 'listener' for errors is needed when the App call resources like 'hardware / IO / ram checking'.

It's not a 'logical error fix' at all.

1 solution

In C#, all indexes are zero based. So if you have 16 rows in your DataGridView, then the valid indexes are 0 to 15 inclusive. Any attempt to access row 16 is doomed to failure, with or without a try...catch block.

A much better solution is to check how many rows exist, and use that number minus one as the "final row" index:
DataGridView.Rows[DataGridView.Rows.Count - 1].Selected = true;

But you shouldn't call your objects the same as the class name - it just gets confusing. Name then according to what they are used for: ShowPictureInfo for example, or perhaps ShowImageSourceLocation perhaps. But not DataGridView!
Share this answer
anthp 1-May-23 19:26pm    
Thank OriginalGriff,

If I may,

the 24 pictureboxes i am loading by pressing a button, eg press button 1, loads the first 24 pictures, press it again it loads the next 24 pictures and so on.

i am achieving this by:

int currentRow2 = DataGridViewPics.CurrentRow.Index;
this.DataGridViewPics.CurrentCell = this.DataGridViewPics[1, (CurrentRow2 + 1)];

this seems to work fine except also when i get to the end of the pictures, which could be 200 could be 500 i get the index out of range error.

is there a better way to achieve my method ?
anthp 1-May-23 19:28pm    
Sorry Dave, I just deleted your post by mistake.
anthp 1-May-23 19:35pm    
Guys this is my code for loading the pictures, i have 24 blocks of this code, 1 for each datagridview row.
Is there a better way to load these pictures?

DataGridViewPics.Rows[21].Selected = true;
lblPicsSelectionLocation22.Text = DataGridViewPics.SelectedCells[0].Value.ToString();
String header21 = DataGridViewPics.SelectedCells[0].Value.ToString();
header21 = header21.Replace("mpg", "jpg").Replace("mpeg", "jpg").Replace("mp4", "jpg");
pictureBoxSongSelection22.ImageLocation = header21;

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