Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

1 solution

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

C#
[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.
 
Share this answer
 
Comments
KarthiSrv 25-Aug-15 6:40am    
i need complete solution for this..can u plz forward it
Member 12556873 2-Jun-16 1:00am    
sdfsdf

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900