Click here to Skip to main content
13,093,720 members (60,333 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 25-Aug-15 6:40am
i need complete solution for this..can u plz forward it
Member 12556873 2-Jun-16 1:00am

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 8 Jul 2013
Copyright © CodeProject, 1999-2017
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