简体   繁体   English

WPF中使用c#的Entity Framework 6中的GridView

[英]GridView in Entity Framework 6 in WPF with c#

I am working in WPF in VS2013 with linkage of database created in SQL Server 2014. I am writing simple functions like add, create, delete and update records. 我正在VS2013中的WPF中工作,并链接了在SQL Server 2014中创建的数据库。我正在编写简单的功能,例如添加,创建,删除和更新记录。 All function runs fine, but I want that all my records should be shown in a GridView in WPF.. 所有功能运行正常,但我希望所有记录都应显示在WPF的GridView中。

I searched a lot but could not find any answer.. I wrote this code for adding records: 我进行了大量搜索,但找不到任何答案。.我编写了以下代码以添加记录:

DataContext dc = new DataContext();
MyNew_DBEntities db = new MyNew_DBEntities();

Student st = new Student();
st.First_Name = First_Name.Text;
st.Last_Name = Last_Name.Text;
st.Department = Department.Text;

db.Students.Add(st);
db.SaveChanges();
MessageBox.Show("Record Added Seccessfuly", "Message", MessageBoxButton.OK, MessageBoxImage.Information);

and I want that after this addition all my record entered should be shown in GridView and so on. 我希望在添加之后,我输入的所有记录都应显示在GridView中,依此类推。 Similarity for deletion and update of records too. 删除和更新记录的相似性。

You are going to want to use your DbContext and get your object graph you want to display on your grid. 您将要使用DbContext并获取要显示在网格上的对象图。

MyNew_DBEntities db = new MyNew_DBEntities();
var StudentCollection = db.Students.ToList();

you can perform a ToList() as shown above and send to the ItemSource of the data grid. 可以执行如上所示的ToList()并将其发送到数据网格的ItemSource。

Then set your databinding on the DataGrid 然后在DataGrid上设置数据绑定

<DataGrid ItemsSource="{Binding StudentCollection}">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="FirstName">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding First_Name}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

However, I would set the ItemSource databinding to an ObservableCollection instead. 但是,我会将ItemSource数据绑定设置为ObservableCollection。 The ObservableCollection will automatically update the UI if the collection changes where the List won't. 如果集合更改列表不会更改的位置,则ObservableCollection将自动更新UI。 The only problem is that you can't easily convert to a ObservableCollection. 唯一的问题是,您不能轻松地转换为ObservableCollection。 You will need to write an extension method that does this work. 您将需要编写一个扩展方法来完成这项工作。 I usually use the following 我通常使用以下

public static class CollectionUtility
{
    public static ObservableCollection<T> ToObservableCollection<T>(
        this IEnumerable<T> enumeration)
    {
        return new ObservableCollection<T>(enumeration);
    }
}

if you implement this extension method now you can use this instead of the .ToList() 如果现在实现此扩展方法,则可以使用它代替.ToList()

var StudentCollection = db.Students.ToObservableCollection();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM