【问题标题】:How I can retrieve image from database into datagrid in WPF with EF如何使用 EF 将图像从数据库检索到 WPF 中的数据网格中
【发布时间】:2016-01-24 14:48:06
【问题描述】:

我是 Entity 框架的新手,所以我需要帮助。我将图像保存到数据库(SQL Server),然后我想将图像检索到数据网格中 这是我的代码:

using(var contxt=new lotteryEntities1())
        {
            var AllPlayer = contxt.GetAllPlayars();
            foreach( var pp in AllPlayer)
            {

                string PName = pp.Player_name;
                string phone = pp.Player_Phone;
                 Photos = ToImage(pp.Photo);
            }

            //DTGridEmp.ItemsSource = pp;
        }   

这是我的 Xaml 代码:

                <DataGridTemplateColumn Header="Image">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Path=Photos}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>   

我正在从我的实体调用该过程,并且需要将所有列绑定到带有图像的数据网格中?

【问题讨论】:

  • 任何帮助做什么?你没有问任何问题!
  • 这是我的代码,我正在尝试将数据库中的图像检索到 datagride 中
  • 嗯?你怎么了?您需要更具体地了解您的问题。
  • 问题是图片没有出现在数据网格中
  • 当然没有人需要知道Photos = ToImage(pp.Photo); 做了什么。

标签: c# wpf entity-framework datagrid


【解决方案1】:

这就是答案

   List<Players> player = new List<Players>();

        using (var contxt = new lotteryEntities1())
        {

            var AllPlayer = contxt.GetAllPlayars();

            foreach(var pp in AllPlayer)
            {
                player.Add(new Players() { PalyerName = pp.Player_name, PlayerPhone = pp.Player_Phone,Pics=pp.Photo});
            }
            DTGridEmp.ItemsSource = player;


            //  DTGridEmp.ItemsSource = items;

        }

Xaml 代码是

<DataGrid x:Name="DTGridEmp"  AutoGenerateColumns="False" Margin="0,13,0,0" Width="Auto" Height="Auto"  Style="{DynamicResource DataGridStyle1}" ColumnHeaderHeight="40"  FlowDirection="RightToLeft" SelectedIndex="0"   ColumnWidth=" 243" IsReadOnly="True" SelectionChanged="DTGridEmp_SelectionChanged" PreviewMouseDown="DTGridEmp_PreviewMouseDown">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding PalyerName}" />
                <DataGridTextColumn Header="TelePhone" Binding="{Binding PlayerPhone}"  />
                <DataGridTemplateColumn Header="Photo" Width="Auto" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Path=Pics}" Width="40" Height="40" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

谢谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多