Click here to Skip to main content
16,016,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to add image in first column of data grid view by coding..my code is
window5.xaml
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" Background="LightGray" RowBackground="LightGray" FontFamily="calibri">
            <dg:DataGrid.ContextMenu>

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


window5.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Data;

namespace WpfApplication5
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        List<Employee> employeeList = new List<Employee>();
        public class Employee
        {
            public int ID { 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
                {
                    ID = i,
                    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;
  }
        private void dgData_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            ContextMenu cxMenu = new ContextMenu();
        }
       
       private void Ass_Lesson1(object sender, RoutedEventArgs e)
        {
            Employee emp = null;
            foreach (Employee item in dgData.SelectedItems)
            {
                emp = new Employee
                {
                    ID = item.ID,
                    FirstName = item.FirstName,
                    LastName = item.LastName,
                    EmailID = item.EmailID,
                    Contact = item.Contact
                };
            }
            MessageBox.Show(emp.ID.ToString());
        }
    }
}
Posted
Updated 30-May-11 8:12am
v2

1 solution

You can try it in this way:

XML
<dg:DataGrid  MouseRightButtonUp="dgData_MouseRightButtonUp" AutoGenerateColumns="True" Margin="27,36,39,50" x:Name="dgData" RowHeight="25" Background="LightGray" RowBackground="LightGray" FontFamily="calibri">
            <dg:DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Add" Click="Ass_Lesson1">
                    </MenuItem>
                </ContextMenu>
            </dg:DataGrid.ContextMenu>
<dg:datagrid.columns xmlns:dg="#unknown">
 <dg:datagridtemplatecolumn header="First Column">
    <dg:datagridtemplatecolumn.celltemplate>
       <datatemplate>
          <Image Source="{Binding Path=MyImage}" />
       </datatemplate>
    </dg:datagridtemplatecolumn.celltemplate>
 </dg:datagridtemplatecolumn>
</dg:datagrid.columns>
          </dg:DataGrid>

Where MyImage is the property which holds you Image(BitmapImage).

Hope it helped.
 
Share this answer
 
v2
Comments
vishal_h 30-May-11 8:27am    
Its giving me error..please there is any other way tel me
Tarun.K.S 30-May-11 9:05am    
Can you tell me what the error is?
vishal_h 31-May-11 1:30am    
errors are..like this
Error 1 The attachable property 'Columns' was not found in type 'DataGrid'. D:\demo\WpfApplication5\WpfApplication5\Window1.xaml 31 14 WpfApplication5
Error 2 The type 'dg:datagridtemplatecolumn' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. D:\demo\WpfApplication5\WpfApplication5\Window1.xaml 33 18 WpfApplication5
Error 3 The attachable property 'celltemplate' was not found in type 'datagridtemplatecolumn'. D:\demo\WpfApplication5\WpfApplication5\Window1.xaml 34 22 WpfApplication5
Error 4 The type 'datatemplate' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. D:\demo\WpfApplication5\WpfApplication5\Window1.xaml 35 26 WpfApplication5
Error 5 The type 'image' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. D:\demo\WpfApplication5\WpfApplication5\Window1.xaml 36 30 WpfApplication5
Tarun.K.S 31-May-11 2:35am    
Work fine for me. As far as the errors are concerned, please note that its DataGridTemplateColumn and not datagridtemplatecolumn. The XAML engine recognizes only like that only. Similarly, its DataTemplate instead of datatemplate, so is it for CellTemplate Image. I will update the answer accordingly. If it helped, do vote and accept the answer.
vishal_h 31-May-11 2:40am    
xaml code is
<grid>
<my:datagrid autogeneratecolumns="False" margin="178,41,175,82" name="dataGrid1" grid.row="1"
="" verticalalignment="Top" itemssource="{Binding}">
<my:datagrid.columns>
<my:datagridtemplatecolumn header="SampleImagess">
<my:datagridtemplatecolumn.celltemplate>
<datatemplate>
<Image Height="25" Width="50" Source="{Binding}" />







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