Click here to Skip to main content
Rate this: bad
good
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 1:37am

1 solution

Rate this: bad
good
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();
dt.Columns.Add("Name");
DataRow dr = dt.NewRow();
dr["Name"] = "tick";
dt.Rows.Add(dr);
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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:wpf="http://schemas.microsoft.com/wpf/2008/toolkit" Loaded="Window_Loaded"
        xmlns:local="clr-namespace:FormWithTab"
    Title="Window2" Height="300" Width="300">
    <window.resources>
        <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}}" />
        </datatemplate>
      
    </window.resources>
    <grid>
        <wpf:datagrid x:name="grid1" verticalalignment="Top" height="150" autogeneratecolumns="False" xmlns:wpf="#unknown">
            <wpf:datagrid.columns>
                <wpf:datagridtemplatecolumn header="Image" celltemplate="{StaticResource ImageColTemplate}" />
            </wpf:datagrid.columns>
        </wpf:datagrid>
      
    </grid>
</window>
 
If you need complete solution for this . i can forward you.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 605
1 Maciej Los 319
2 BillWoodruff 184
3 Richard MacCutchan 180
4 Suraj Sahoo | Coding Passion 180
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


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