Click here to Skip to main content
16,021,288 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
I have used WPF toolkit for data grid in my application where i have five column ICON,FirstName,LastName,EmailID,Contact..and i want to add image .png in ICON column at run time in .cs file...i am not getting how to add image in only that column..
my xaml code is..
<pre lang="xml"><Window x:Class="WpfApplication5.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window1" Height="450" Width="737" Loaded="Window_Loaded"
        xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Window.Resources>
        <Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="LightBlue" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="#80EEEEEE" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid Height="374" Width="554">
        <dg:DataGrid  MouseRightButtonUp="dgData_MouseRightButtonUp" AutoGenerateColumns="True" Margin="27,36,39,50" x:Name="dgData" RowHeight="25">
            <dg:DataGrid.ContextMenu>

                <ContextMenu>
                    <MenuItem Header="Add" Click="Ass_Lesson1">
                    </MenuItem>
                </ContextMenu>
            </dg:DataGrid.ContextMenu>
        </dg:DataGrid>
    </Grid>
</Window

>



my .cs code is

public partial class Window1 : Window
   {
       List<Employee> employeeList = new List<Employee>();
       public class Employee
       {
           public string ICON { get; set; }
           public string FirstName { get; set; }
           public string LastName { get; set; }
           public string EmailID { get; set; }
           public string Contact { get; set; }
       }
       public Window1()
       {
           InitializeComponent();


           for (int i = 1; i <= 5; i++)
           {
               Employee emp = new Employee
               {
                   ICON = @"D:\play.png",
                   FirstName = "FirstName" + i.ToString(),
                   LastName = "LastName" + i.ToString(),
                   EmailID = "FirstName " + i.ToString() + ".LastName" + i.ToString() + "@some.com",
                   Contact = "9999999" + i.ToString()
               };
               employeeList.Add(emp);
           }
           dgData.ItemsSource = employeeList;


       }
Posted

1 solution

I'm not sure you can using autogenerate columns, it certainly won't work if your ICON property is a string value.

Try changing the ICON property to a Uri ICON = new URI(stringvalue,relativeorabsolute); this will atleast tell the datagrid what datatype to expect!
 
Share this answer
 
Comments
vishal_h 31-May-11 8:15am    
its not work..sir i have posted question becouse i added the image in grid but it added in all column..

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