Click here to Skip to main content
15,913,487 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

I am using this code for image display but same images are displaying in all rows, but my images are different for every row.
C#
private void CreateColumns()
        {
            DataGridViewImageColumn imageColumn;
            Bitmap bmpImage = null;
            imageColumn = new DataGridViewImageColumn();
            
            for (int i = 0; i < dgvDisplayTiles.Rows.Count;i++ )
            {

                bmpImage = (Bitmap)Image.FromFile(Application.StartupPath + dgvDisplayTiles.Rows[i].Cells[3].Value.ToString(), true);
                imageColumn.Image = bmpImage;
                imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
                
                //dgvDisplayTiles.Rows.Add();
                dgvDisplayTiles.Rows[i].Cells[3].Value = bmpImage;
                dgvDisplayTiles.Rows[i].Height = 100;
                
            }
            dgvDisplayTiles.Columns.Add(imageColumn);

        }

Please help me, how can display different type of images in all rows.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted
Updated 7-Dec-13 2:19am
v4

You are assigning each loaded image to the same value "imageColumn.Image" and then trying to assign bmpImage to dgvDisplayTiles.Rows[i].Cells[3].Value that presumably is a TextColumn (not good). Add imageColumn to the dgvDisplayTiles before your loop. Delete the assigment to "imageColumn.Image"
Then change your assignment to:
dgvDisplayTiles.Rows[i].Cells[imageColumn.DisplayIndex].Value = bmpImage;
 
Share this answer
 
Hey Ankit try This,
C#
private void CreateColumns()
    {
        var imageColumn = new DataGridViewImageColumn();

        object dgvDisplayTiles;
        for (var i = 0; i < dgvDisplayTiles.Rows.Count; i++)
        {
            var bmpImage = (Bitmap)Image.FromFile(Application.StartupPath + dgvDisplayTiles.Rows[i].Cells[3].Value.ToString(), true);
            imageColumn.Image = bmpImage;
            imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
            imageColumn.Dispose();
            //dgvDisplayTiles.Rows.Add();
            dgvDisplayTiles.Rows[i].Cells[3].Value = bmpImage;
            dgvDisplayTiles.Rows[i].Height = 100;
            dgvDisplayTiles.Columns.Add(imageColumn);
            
        }
    }
 
Share this answer
 
v4
Comments
[no name] 7-Dec-13 8:37am    
Sorry buddy, same issue.
Problem not solved.
[no name] 7-Dec-13 8:42am    
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Vishal Pand3y 7-Dec-13 8:48am    
sry for previous answer see my updated Solution
actually what you are doing is adding only 1 image in that grid move last line in the loop it will solve your problem :)
Vishal Pand3y 7-Dec-13 9:05am    
is this working if not let me know @ankit...
[no name] 9-Dec-13 0:16am    
i m using this code but it's showing error:-
var imageColumn = new DataGridViewImageColumn();

for (var i = 0; i < dgvDisplayTiles.Rows.Count; i++)
{
var bmpImage = (Bitmap)Image.FromFile(Application.StartupPath + dgvDisplayTiles.Rows[i].Cells[3].Value.ToString(), true);
imageColumn.Image = bmpImage;
imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
imageColumn.Dispose();
dgvDisplayTiles.Rows[i].Cells[3].Value = bmpImage;
dgvDisplayTiles.Rows[i].Height = 100;
dgvDisplayTiles.Columns.Add(imageColumn);
}

Error:- Provided column already belongs to the DataGridView control.
use following before for loop start a new loop or before assigning a value to bmpImage in starts of for loop

C#
bmpImage.dispose();


or

C#
bmpImage = null;
 
Share this answer
 
Comments
[no name] 7-Dec-13 8:32am    
I have writen this code but my problem not solved.
private void CreateColumns()
{
DataGridViewImageColumn imageColumn;
Bitmap bmpImage = null;
imageColumn = new DataGridViewImageColumn();

for (int i = 0; i < dgvDisplayTiles.Rows.Count;i++ )
{
if (bmpImage != null)
{
bmpImage.Dispose();
}
bmpImage = (Bitmap)Image.FromFile(Application.StartupPath + dgvDisplayTiles.Rows[i].Cells[3].Value.ToString(), true);
imageColumn.Image = bmpImage;
imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
dgvDisplayTiles.Rows[i].Cells[3].Value = bmpImage;
dgvDisplayTiles.Rows[i].Height = 100;

}
dgvDisplayTiles.Columns.Add(imageColumn);

}

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