2013-01-17 51 views
0

這是我的數據網格,我想在網格列中顯示圖像,但它顯示的是文本System.Windows.Media.Imaging.BitmapImage,而不是顯示圖像。 如何在datagrid列中添加圖片?在Datagrid中添加圖像

這是我曾嘗試:

Dictionary dict1=new Dictionary < string,object>(); 

string keyimage="Image"; 

object a1 = new BitmapImage(); 

a1=Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Information.Handle, Int32Rect.Empty, null); 

dict1.Add(keyimage, a1); 

dict1作爲Itemsource爲DataGrid ...........如何使顯示在DataGrid列的圖像?

+0

什麼列類型:

由於您使用的字典,結合,有點像? – Dennis

+0

這是我的問題..........我應該使用什麼列類型來顯示圖像? – Raghu

+0

你會在這本詞典中找到什麼,只有圖片? –

回答

2

請確保您使用的是DataGridTemplateColumn並將圖像放入其中。您使用的顯示圖像

<Image Binding="{Binding Height="25" Width="50" Source="{Binding Value}" /> 
0

我想你的列數據類型與itemsource數據類型不匹配。您必須爲網格列和項目源創建相同的數據類型圖像。 在我的WPF項目我做了圖片我列數據模板:

DataTable dtab = new DataTable(); 
dtab.Columns.Add("imageColumn", typeof(BitmapImage)); 
//Uploading dtab with Images from DataBase 

FrameworkElementFactory factory = new FrameworkElementFactory(typeof(Image)); 
Binding bind = new System.Windows.Data.Binding("imageColumn"); 
factory.SetValue(Image.SourceProperty, bind); 
DataTemplate cellTemplate = new DataTemplate() { VisualTree = factory }; 
DataGridTemplateColumn imgCol = new DataGridTemplateColumn() 
{ 
    Header="image", 
    CellTemplate = cellTemplate 
}; 
DataGrid dg = new DataGrid(); 
dg.Columns.Add(imgCol); 
dg.ItemsSource = dtab.DefaultView; 

希望這有助於

0

任何圖像添加到您的項目上的資源文件夾

之後嘗試在DataGrid綁定下面的編碼事件。在這裏我添加圖像標誌...

DataGridViewImageColumn imageColoumn = new DataGridViewImageColumn(); 
     imageColoumn.HeaderText = "Img"; 
     imageColoumn.Image = null; 
     imageColoumn.Name = "DataPic"; 
     imageColoumn.Width = 150; 
     dataGridView1.Columns.Add(imageColoumn); 

     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
      DataGridViewImageCell cell = row.Cells[1] as DataGridViewImageCell; 
      cell.Value = (System.Drawing.Image)Properties.Resources.logo; 
     } 
+0

DataGridViewImageColumn是否有任何命名空間或什麼以及更多我正在使用wpf datagrid而不是datagridview – Raghu

+0

好吧,那麼你只是試試這個鏈接.... http://stackoverflow.com/questions/7938779/image-column-in-wpf-datagrid ... – Pandian

+0

我已經設置AutoGenerateColumns =「真」,因爲我有一個非常好的數據集,我可以很容易地綁定它與數據網格,但現在我想添加一個圖像在datadrid不顯示數據集...... xaml沒有變化只有我能做的事情是在c#代碼中做的事情...... .... – Raghu