0

This is my datagrid i want to display an image in the grid column but its is showing me text System.Windows.Media.Imaging.BitmapImage, instead of displaying an Image. How to add an image in a datagrid column?

This is what I have tried :

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);

This dict1 serves as Itemsource for the datagrid...........how to make an image to be displayed in a datagrid column?

noobob
  • 532
  • 4
  • 12
Raghu
  • 313
  • 2
  • 7
  • 19

3 Answers3

2

Make sure you use a DataGridTemplateColumn and put an image into it.

Since you are using a dictionary, bind the value, something like:

<Image Binding="{Binding Height="25" Width="50" Source="{Binding Value}" />
noobob
  • 532
  • 4
  • 12
0

I suppose your Column datatype mismatches with itemsource datatype. You have to make equal datatype Image for both Grid Column and itemsource. In my WPF project I made my column data template with Images:

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;

Hope this help

Alex
  • 8,827
  • 3
  • 42
  • 58
0

Add any image to your Project on Resource folder

After that try the below coding in DataGrid Bind event. Here i have add the image logo...

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;
        }
Pandian
  • 8,848
  • 2
  • 23
  • 33
  • DataGridViewImageColumn does it have any namespace or something and more over i am using wpf datagrid and not datagridview – Raghu Jan 17 '13 at 12:20
  • ok then you just try this link.... http://stackoverflow.com/questions/7938779/image-column-in-wpf-datagrid... – Pandian Jan 17 '13 at 12:30
  • i have set AutoGenerateColumns="True" since i have a very good dataset and i could easily bind it with the datagrid but now i am trying to add a image in the dataset which the datadrid is not showing......no changes in xaml only thing i could do is to do something in c# code .......... – Raghu Jan 17 '13 at 12:38