Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 986
Gisiota Avatar asked Jul 10 '15 11:07

Gisiota


People also ask

Which class is used for data binding in WPF?

WPF data binding supports data in the form of CLR objects and XML. To provide some examples, your binding source may be a UIElement, any list object, a CLR object that is associated with ADO.NET data or Web Services, or an XmlNode that contains your XML data.

How many types of binding are there in WPF?

WPF binding offers four types of Binding.


1 Answers

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;
like image 96
octavioccl Avatar answered Oct 13 '22 07:10

octavioccl