7

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>

4 Answers 4

7

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;
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the info, a bit confused on where I should add the code to materialize the query. I tried under window_Loaded and under public Form1() and when I type in the CEntities to load the table the only options im give are Equals and ReferenceEquals.
No worries, thanks for all your help. I feel stupid, the datagrid now shows the field types (such as date picker and text box, before it was all just blank), but the data is still not present. I think it has to do with my query, I never use the variable permits anywhere and not sure how to bind it if Im doing the context.tblper.local method. I updated my window_loaded code with what I currently have.
Are your sure you have data in that table? Two things, you are not calling the Load() method and you don't have to set the DataContext and the ItemSource at the same time, choose one, if you choose set the ItemSource of your grid, then delete ItemsSource="{Binding}" from your xaml code.
Thanks for all your help! I wasn't loading. I needed to reference it in the code (using project.model). Id like to bind this data using only XAML, is there any literature or sites you recommend to read up on this?
+1000! There's no Load() with (EF7 / EF Core) but ToList() worked. Thanks for including the filter example, it's what I needed!
1

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.

Comments

0

If you set AutoGenerateColumns="False" you should provide the columns definition on XAML or set AutoGenerateColumns="True". This is the first thing to do.

Comments

0

If You want to use Model (also ItemsSource="{Binding}" is a hint for me, that You want), than don't hard code the ItemsSource. You create the Model's object in a CEntities context; variable, but You should set it to the DataContext property of the Window like this:

DataContext = new CEntities();

and remove the line

tBLPERDataGrid.ItemsSource = permits;

Now the ItemsSource is coming from the CEntities instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.