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

I am developing a window application.
At the time of rendering images in DataGridViewImageColumn , only first image is showing in all rows.This is my code...
C#
private void BindGrid()
{
    DataSet ds = objStaff.GetStaff(1);

    if (ds != null)
    {
        grdDetail.DataSource = ds.Tables[0];
        grdDetail.Columns["StaffId"].Visible = false;
        grdDetail.Columns["Image"].Visible = false;
        DataGridViewImageColumn img = new DataGridViewImageColumn();
        //int number_of_rows = grdDetail.RowCount;
    
        for (int i = 0; i < grdDetail.Rows.Count; i++)
        {
            img.HeaderText = "Image";
            img.Width = 80;      
            string imagePath = ds.Tables[0].Rows[i][1].ToString();
            Image image = Image.FromFile(imagePath);
            img.Image = image;        
            
            //grdDetail.Rows[i].Cells["Picture"].Value = image;
             grdDetail.Columns.Add(img);
        }
    
        grdDetail.Refresh();
    }
}


What should I do? Please help.
Posted
v5
Comments
OriginalGriff 8-Jul-13 4:42am    
You could try giving us a bit more information?
This is not a good question - we cannot work out from that little how you are trying to do what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
jaideepsinh 8-Jul-13 5:52am    
First of all use break point at this line :
string imagePath = ds.Tables[0].Rows[i][1].ToString();
And check you get different imagePath or same all then time.And check you grid view how you use image control in it.

1 solution

You Can : (its works for me)
1. add a byte column to table of dataset
2. Add image in byte column
3. then bind data table with grid
as follows


C#
private void BindGrid()
{
    DataSet ds = objStaff.GetStaff(1);
 
    if (ds != null)
    {
            DataTable dtable = new DataTable();
            DataColumn dcol = new DataColumn("myImage");
          
            dtable = ds.Tables[0].Copy;
            dcol.AllowDBNull = true;
            dcol.DataType = System.Type.GetType("System.Byte[]");
            dcol.Caption = "myImage";

            dtable.Columns.Add(dcol);
            foreach (DataRow drow in dtable.Rows)
            {
               string imagePath = drow[1].ToString();
	       Image image = Image.FromFile(imagePath);
	       System.IO.MemoryStream ms = new System.IO.MemoryStream();
	       image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); // your image format here
	       drow["myImage"] = ms.ToArray();
            }

        grdDetail.DataSource = dtable;
        grdDetail.Columns["StaffId"].Visible = false;
        grdDetail.Columns["Image"].Visible = false;
    }
}     
 
Share this answer
 
v5
Comments
manika.iise 8-Jul-13 8:11am    
Thanx for reply...
where will this Cell be placed? and what is the purpose of "img" ?
actually this code is not working.

img.HeaderText = "Image";
img.Width = 80;

grdDetail.Columns.Add(img);

for (int i = 0; i < grdDetail.Rows.Count; i++)
{

DataGridViewImageCell ImgCell = new DataGridViewImageCell();
string imagePath = ds.Tables[0].Rows[i][1].ToString();
//Set image value
ImgCell.Value = Image.FromFile(imagePath);
//Add the image cell on row
grdDetail.Rows(i).Cells.Add(ImgCell);

}

Surendra Adhikari SA 10-Jul-13 2:05am    
i updated the solution ...did you solved this?

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