2015-06-15 112 views
0

我有一個DataTable包含以下幾列:更改DataGridView中的細胞圖像

  1. 編號
  2. 的ImageIndex
  3. 來源
  4. 目標

在我的表單的加載事件我想用我的填充我的DataGridView 210。但是我還想要一個圖像列,根據我的DataTable上的ImageIndex的值顯示不同的圖像。

我該怎麼做?

回答

0

第一步是將圖像添加到屬性文件夾下的Resources.resx文件中。然後在DataGridView中添加一個DataGridViewImageColumn。

最後在GridView的databound事件中使用這個代碼片段。

for (int row = 0; row <= [YourDataGridViewName].Rows.Count - 1; row++) 
{ 
    if(gvFiles.Rows[row].Cells["Index of the imageindexcolumn"]).Value = 1) 
     { 
      (DataGridViewImageCell)gvFiles.Rows[row].Cells["Index of the imagecolumn"]).Value = Properties.Resources.Picture1 
     } 
    else if (gvFiles.Rows[row].Cells["Index of the imageindexcolumn"]).Value = 2) 
     { 
      (DataGridViewImageCell)gvFiles.Rows[row].Cells["Index of the imagecolumn"]).Value = Properties.Resources.Picture2 
     } 
} 
+1

如果圖像已被導入到Resources.resx中,這將工作正常。我的猜測是,儘管圖像可能來自存儲在數據庫中的URL。如果是這種情況,請查看:http://stackoverflow.com/questions/10759772/how-to-show-image-from-url-in-datagridview-cell –