Click here to Skip to main content
11,803,561 members (67,172 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# WPF wpftoolkit
Hello everyone
I'm working with the WPF-Datagrid
I need to add an image field to datagrid from database. Image stored in table in string type - this is file name only('*.png'). I have to convert it to bitmap and add to datagrid with other text fields from table.
any suggestions?
(.NET 3.5; WPF Toolkit feb 2010 3.5.50211)
Posted 2-Jul-13 0:37am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I Hope following solution will help you

Key Features:

1) DataTable : to store data from database and to bind this data with data grid
2) ValueConverter : Convert column value to specified image source
3) DataTemplate : Template for grid column having image type

1) DataTable : here i have used static table you can fetch data from database

DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dr["Name"] = "tick";
grid1.ItemsSource = null;
grid1.ItemsSource = dt.DefaultView;

2) Value Converter

[ValueConversion(typeof(string), typeof(SolidColorBrush))]
      public class StringConverter : IValueConverter
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
              string val = value.ToString();
              return "/FormWithTab;component/Images/" + val + ".png";

          public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
              throw new NotSupportedException();

3) DataTemplate

<pre><window x:class="FormWithTab.Window2" xmlns:x="#unknown">
         xmlns:wpf="" Loaded="Window_Loaded"
    Title="Window2" Height="300" Width="300">
        <local:stringconverter x:key="NameToStringConverter" xmlns:local="#unknown" />
        <datatemplate x:key="ImageColTemplate">
            <image x:name="img" stretch="None" horizontalalignment="Center" source="{Binding Name,  Converter={StaticResource NameToStringConverter}}" />
        <wpf:datagrid x:name="grid1" verticalalignment="Top" height="150" autogeneratecolumns="False" xmlns:wpf="#unknown">
                <wpf:datagridtemplatecolumn header="Image" celltemplate="{StaticResource ImageColTemplate}" />

If you need complete solution for this . i can forward you.
KarthiSrv at 25-Aug-15 6:40am
i need complete solution for this..can u plz forward it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 375
1 CPallini 290
2 F-ES Sitecore 270
3 Sergey Alexandrovich Kryukov 234
4 CHill60 220
0 OriginalGriff 3,000
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,755
4 Richard MacCutchan 1,187

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 8 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100