0

I want to show image (16*16px png file) in DataGridView. the header name of path pathname for each logo is "logo_dom", I compiled this and I have a text in each rows "System.Drawing.Bitmap" instead of logo picture.

        private void button1_Click(object sender, EventArgs e)
    {

        DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
        DataGridViewImageCell imageCell = new DataGridViewImageCell();
        Bitmap bmpImage = null;


        int number_of_rows = dgv_resultats.RowCount;
        for (int i = 0; i < number_of_rows-1; i++)
        {

            //bmpImage = (Bitmap)Image.FromFile(Application.StartupPath + dgv_resultats.Rows[1].Cells[1].Value.ToString(), true);

            bmpImage = (Bitmap)Image.FromFile(@"D:\LigueStats\Data\Logo\Ligue 1\EST.png", true);
            imageColumn.Image = bmpImage;

            imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;

            dgv_resultats.Rows[i].Cells["logo_dom"].Value = bmpImage;



        }



    }
Adly 007
  • 3
  • 4

2 Answers2

3

Try this:

DataGridViewImageColumn column = new DataGridViewImageColumn();
this.dataGridView1.Columns.Add(column);                      
string path = @"D:\LigueStats\Data\Logo\Ligue 1\EST.png";
Image img = Image.FromFile(path);
this.dataGridView1.Rows[0].Cells[0].Value = img;

EDIT:

string path = @"D:\LigueStats\Data\Logo\Ligue 1\EST.png";
Image img = Image.FromFile(path);
for (int i = 0; i < dgv_resultats.Rows.Count; i++)
{
    dgv_resultats.Rows[i].Cells["logo_dom"].Value = img;
}
  • error message: System.ArgumentException: Unable to cast an object of type System.Drawing.Bitmap kind System.IConvertible ..... it adds a second column, but the goal is to display the image in a column that already exists appointed "logo_dom" – Adly 007 Nov 04 '15 at 13:01
  • Always try to first adapt the code, not just copy paste. Take a look at the edit of my answer. – Nataniel Richardt Nov 04 '15 at 13:16
  • I think it lacked a little something, it displays " System.Drawing.Bitmag" in each cell instead pictures – Adly 007 Nov 04 '15 at 13:44
  • Take a look at your column "logo_dom" and certificate it is of `DataGridViewImageColumn` type. I just tested this code and it works fine. – Nataniel Richardt Nov 04 '15 at 13:50
  • i'm sorry , how to declare this !! – Adly 007 Nov 04 '15 at 14:06
  • i try this and no changes: DataGridViewImageColumn imageColumn = new DataGridViewImageColumn(); imageColumn.DataPropertyName = "logo_dom"; – Adly 007 Nov 04 '15 at 14:34
  • You're using `DataPropertyName`. It's just `Name` the correct property. – Nataniel Richardt Nov 04 '15 at 15:42
0

the problem is in the type of the column in the datagridview, I ask you how I can change the type of loading the datagridview

the colmunType

Adly 007
  • 3
  • 4