0

I am binding a wpf datagrid data by using given below xaml code.

<DataGrid Height="560" x:Name="dgTest1" VerticalAlignment="Top" ColumnHeaderHeight="41" CanUserResizeRows="False" CanUserResizeColumns="False" CanUserReorderColumns="False" HorizontalAlignment="Stretch" CanUserDeleteRows="False" CanUserAddRows="False" VerticalContentAlignment="Center" FontSize="13" FontFamily="Arial" GridLinesVisibility="Horizontal" HorizontalGridLinesBrush="#FFB2ACAC"  ScrollViewer.CanContentScroll="False" RowHeight="25" ItemsSource="{Binding}" Visibility="Visible" AlternatingRowBackground="WhiteSmoke" AlternationCount="1" IsReadOnly="True" SelectionUnit="FullRow">
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="BorderThickness" Value="0"/>
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightSteelBlue"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
            </DataGrid.Resources>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="Background" Value="LightGray"/>
                    <Setter Property="Foreground" Value="Black" />
                </Style>
            </DataGrid.ColumnHeaderStyle>
        </DataGrid>

& I'm binding a values of this datagrid in code behind like,

void CreateDataTable()
    {
        try
        {

        DataTable dtGlossary = new DataTable();
        DataColumn dcIcon1 = new DataColumn("Icon1");


        //dcIcon1.DataType = System.Type.GetType("System.Drawing.Bitmap");
        dcIcon1.AllowDBNull = true;

        dtGlossary.Columns.Add(dcIcon1);
        dtGlossary.Columns.Add("Desc1");

        DataColumn dcIcon2 = new DataColumn("Icon2");
        //dcIcon2.DataType = System.Type.GetType("System.Drawing.Bitmap");
        dcIcon2.AllowDBNull = true;

        dtGlossary.Columns.Add(dcIcon2);
        dtGlossary.Columns.Add("Desc2");

            AddNewRow(ref dtGlossary, global::Project1.Properties.Resources.play, "Playback a Test Case", global::MMTAS_WPF.Properties.Resources.abortedBadge, "Test Case Was Aborted");
            AddNewRow(ref dtGlossary, global::Project1.Properties.Resources.record_disabled, "Recording Disabled During Playback", global::MMTAS_WPF.Properties.Resources.willNotRunBadge, "Test Case Will Not Run");                
            dgTest1.DataContext = dtGlossary.DefaultView;
        }
        catch
        {

        }            
    }

 private void AddNewRow(ref DataTable pDataTable, System.Drawing.Bitmap pIcon1, string pDesc1, System.Drawing.Bitmap pIcon2, string pDesc2)
    {
        DataRow dr = pDataTable.NewRow();
        dr["Icon1"] = pIcon1;
        dr["Desc1"] = pDesc1;
        dr["Icon2"] = pIcon2;
        dr["Desc2"] = pDesc2;
        pDataTable.Rows.Add(dr);
        pDataTable.AcceptChanges();
    }

But, the images is not binding to datagrid, the output is displaying like, Output

I want to display images to Datagrid columns by using DataTable. How can i solve this issue ? Thanks in advance.

Manikandan Sethuraju
  • 2,873
  • 10
  • 29
  • 48
  • i would say you need a datagridtemplatecolumn to show your images - see this http://stackoverflow.com/questions/14432313/add-a-image-in-the-datagridtemplatecolumn – blindmeis Nov 25 '13 at 13:03
  • Is there anyway to show images without using 'datagridtemplatecolumn' ? i'm calling the images from resource file .. – Manikandan Sethuraju Nov 25 '13 at 13:07
  • wpf dont know how to show your image. thats why you need a templatecolumn and should use binding – blindmeis Nov 25 '13 at 13:42

0 Answers0