4

I want to add two columns in wpf datagrid one image & one text columns dynamically.

Xaml code :

 <Grid><DataGrid AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="grid" VerticalAlignment="Stretch" Width="Auto" ></DataGrid></Grid>

Code Behind:

 DataGridTextColumn col = new DataGridTextColumn();
  col.Header =Text1;
  col.Binding =Text1;
  grd.Columns.Add(col);

How do I add image column?or show image in the column?

Please suggest

Dee

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
dee
  • 43
  • 1
  • 1
  • 3

3 Answers3

6

As Anvaka said, you can Use DataGridTemplateColumn. In C# you can add create DataGridTemplateColumn as this, Here i have added a CheckBox in to the DataGridTemplateColumn.

DataGridTemplateColumn col1 = new DataGridTemplateColumn();
col1.Header = "MyHeader";
FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(Image));
Binding b1 = new Binding("Picture");
b1.Mode = BindingMode.TwoWay;
factory1.SetValue(Image.SourceProperty, b1);
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factory1;
col1.CellTemplate = cellTemplate1;
datagrid.Columns.Add(col1);

Here Picture is a property of ImageSource type in the class which collection is assigned to ItemsSource of DataGrid.

slava
  • 1,901
  • 6
  • 28
  • 32
viky
  • 17,275
  • 13
  • 71
  • 90
  • I have tried this code.But it doesn't work for Image. FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(Image)); What else can I try? – dee Dec 23 '09 at 13:12
  • 1
    Got this exception 'Image' type must derive from FrameworkElement, FrameworkContentElement, or Visual3D. – dee Dec 23 '09 at 14:06
  • Your code is working.It has make the life easier.Thanks alot. – dee Dec 24 '09 at 10:45
0

Use DataGridTemplateColumn. Define cell template in Window.Resources and use FindResource() to set column's CellTemplate property.

Hope this helps.

Anvaka
  • 15,658
  • 2
  • 47
  • 56
  • 1
    Hi Anvaka, Thanks for quick reply.Can you please give me some example or code? – dee Dec 23 '09 at 10:16
  • The links provided by you contains xaml code.I need to add the columns in the code behind(c#). – dee Dec 23 '09 at 12:35
0

If you want to Set an Image in a DataGrid Column HEADER, only programmatically, you can perform like this:

ImageSource image = new BitmapImage(new Uri(@"C:/téléchargement.jpg", UriKind.RelativeOrAbsolute));

Style style = new Style(typeof(DataGridColumnHeader));
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(Image));
factory.SetValue(Image.SourceProperty, image);
factory.SetValue(Image.StretchProperty, Stretch.Uniform);
style.Setters.Add(new Setter { Property = TemplateProperty, Value = new ControlTemplate { TargetType = typeof(DataGridColumnHeader), VisualTree = factory } });

DataZone.Columns[5].HeaderStyle = style;

You can use this method for any type ( Ex : TextBlock , Label, ...), or create a more complex controlTemplate

Eros Guil
  • 31
  • 10