Binding data to a data-grid in WPF with Entity Framework

c# data-binding entity-framework wpf

Question

I've looked all over the web for this, and nothing I found seems to help.

I made a model and added the model to a data source as an object. I assumed it would work like a data set where I can just drag and drop onto a form and it would bind the data for me. But it keeps showing blank when I drag and drop from the model. so I looked online and saw that some code-behind was required and this is what I have and its still blank. Any ideas what Im doing wrong?

   public partial class form1: Window

{
    ComEntities context;;
    public form1()
    {
        InitializeComponent();

    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        context = new ComEntities();

        System.Windows.Data.CollectionViewSource comEntitiesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("comEntitiesViewSource")));


        var permits = (from c in context.tBLPER.Local select c);

        this.DataContext = context.tBLPER.Local;
        tBLPERDataGrid.ItemsSource = context.tBLPER.Local;

    }


}

XAML:

 <DataGrid x:Name="tBLPERDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="10,10,10,413" ItemsSource="{Binding}" EnableRowVirtualization="True" AutoGenerateColumns="False">

    </DataGrid>
1
5
7/10/2015 1:33:06 PM

Accepted Answer

You need to materialize your query (bring the data to memory). You can do that calling the ToList() method, but even better is do this:

 context.TBLPER.Load();  
 this.DataContext = context.TBLPER.Local;  // set the Window DataContext property

Local property gets an ObservableCollection<T> that represents a local view of all Added, Unchanged, and Modified entities in this set. This local view will stay in sync as entities are added or removed from the context. Likewise, entities added to or removed from the local view will automatically be added to or removed from the context.

In case you need to filter your data before (suppose your entity has a property named Age and want the users older than 20), then you can do this:

 context.TBLPER.Where(t=>t.Age>20).Load();  
 this.DataContext = context.TBLPER.Local; 

Another thing, if you want to set the ItemSource property of your Grid in the code behind of your window, it don't make sense create a binding to that property in your xaml code, so remove it:

<DataGrid ... ItemsSource="{Binding}" ...>

If you are going to do this:

tBLPERDataGrid.ItemsSource=context.TBLPER.Local;
4
7/10/2015 1:01:04 PM

Popular Answer

You shouldn't be setting ItemsSource twice (just set it in your code behind - remove ItemsSource="{Binding}").

Also, you should set AutoGenerateColumns="True" because without that you need to add DataGridXColumn elements to the DataGrid.

Have a look here for more details ... http://www.wpf-tutorial.com/datagrid-control/custom-columns/

You may also want to put a breakpoint on the tBLPERDataGrid.ItemsSource = permits; line so you can inspect permits to confirm it contains the data you expect.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow