2015-11-03 79 views
0

我想在DataGridView中顯示圖像(16 * 16px png文件)。每個徽標的路徑路徑名的標題名稱是「logo_dom」,我編譯了這個,並且在每行「System.Drawing.Bitmap」中都有一個文本,而不是徽標圖片。顯示DataGridView中的圖像不能正常工作

 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; 



     } 



    } 

回答

1

試試這個:

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; 

編輯:

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; 
} 
+0

錯誤消息: System.ArgumentException:無法轉換System.Drawing.Bitmap類型的對象類System.IConvertible ..... 它添加了第二列,但目標是在列中顯示圖像已經存在指定「logo_dom」 –

+0

總是嘗試先調整代碼,而不是複製粘貼。看看我的答案的編輯。 –

+0

我要去嘗試 –

0

問題是在DataGridView中列的類型,我問你,我怎麼可以改變的載荷類型datagridview

colmunType