如何使用EF将数据从数据库检索到WPF中的数据网格

时间:2016-01-24 14:48:06

标签: c# wpf entity-framework datagrid

我是实体框架的新手,所以我需要帮助。我将图像保存到数据库(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>   

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

1 个答案:

答案 0 :(得分:0)

这是答案

   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>

全部谢谢