Click here to Skip to main content
15,997,532 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I am trying to add images in a col, 10 images in each row and no of images depends on no of records fetched from database
C#
List<Image> lstImage = new List<Image>();
            for (int i = 0; (i < recLeft) ; i++)
            {
                    Image img = Image.FromFile( <complete path="">"tmp.jpg");
                   
                    Image newImg = img.GetThumbnailImage(_imageSize, _imageSize, null, IntPtr.Zero);
                    
                    lstImage.Add(newImg);
            }

            for (int row = 0; row < numRows; row++)
            {
                DataGridViewRow dRow = new DataGridViewRow();

                recLeft = numRec - (numColumnsForWidth * row);
                

                for (int col = 0; (col < recLeft) && (col < numColumnsForWidth); col++)
                {
                        this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                        dataGridView1.Rows[row].Cells[col].Value= lstImage[0];
}
dataGridView1.Rows.Add();
}


I am getting error at
C#
dataGridView1.Rows[row].Cells[col].Value= lstImage[0];

"index out of range" even if i substitute row and col in above line with 0, which means trying to add atleast one image in 1st col and 1st ro then again error is same.


If anybody can put light on this.
Thanks
Posted
Updated 9-Jul-12 1:16am
v2

1 solution

even if i substitute row and col in above line with 0, which means trying to add atleast one image in 1st col and 1st ro then again error is same
Based on your code, it looks like you are adding rows to grid at runtime. Now, unless until row is already added to the grid, you cannot access the row for any value assignment.

Your final step adds the row to grid whereas you try to access the same row before this addition (where you are trying to assign the image!)

Correct your logic. First add the row and then add the image.
 
Share this answer
 
Comments
Member 8655072 9-Jul-12 7:33am    
Thks Sandeep for answering but if i do like this
for (int row = 0; row < numRows; row++)
{
dataGridView1.Rows.Add();

recLeft = numRec - (numColumnsForWidth * row);


for (int col = 0; (col < recLeft) && (col < numColumnsForWidth); col++)
{
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.Rows[row].Cells[col].Value= lstImage[0];
}
}

where i am adding the row everytime for row loop the putting values of each col in that row.
the problem is same.
Sandeep Mewara 9-Jul-12 14:01pm    
"dataGridView1.Rows.Add();" -> add what?

Try: dataGridView1.Rows.Add(drow);

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