Tom
Tom

Reputation: 4087

WPF databinding to datagrid and entity framework database first

With Database First approach i've created the entities (with entity framework) inside my wpf project so i have the edmx file.

From MSDN: EF generates code from your model using T4 templates. The templates shipped with Visual Studio or downloaded from the Visual Studio gallery are intended for general purpose use. This means that the entities generated from these templates have simple ICollection properties. However, when doing data binding using WPF it is desirable to use ObservableCollection for collection properties so that WPF can keep track of changes made to the collections. To this end we will to modify the templates to use ObservableCollection.

So i've followed this tutorial to change the entities to have ObservableCollection properties: http://msdn.microsoft.com/en-us/data/jj574514.aspx (Section Updating code generation for data binding)

In the WPF view (in the xaml file) with Visual Studio i've added a DataGrid and added this code:

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
   using (SIEntities siContext = new SIEntities())
   {
      var query = from p in siContext.Customers
                  select p;

      dataGrid.ItemsSource = query.ToList();
    }
}

In a first istance, to learn how insert data, i want from code insert a new customer in the database so i've this method:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
   using (SIEntities siContext = new SIEntities())
   {
         Customer cust1 = new Customers();
         cust1.Name = "Pippo";
         cust1.City = "London";
         siContext.Customers.Add(cust1);
         siContext.SaveChanges();
         dataGrid.Items.Refresh();
    }
 }

With this code i'm able to insert a new row in the database but i don't see this new row in the datagrid.

in the xaml file i've the following for datagird:

<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="43,65,0,0" VerticalAlignment="Top" Height="234" Width="423"/>

Why? The dataGrid it's not binding to the entity? How can i show the new row added in the db also in the datagrid?

Thanks

Upvotes: 2

Views: 7189

Answers (1)

Lee O.
Lee O.

Reputation: 3312

I would change your method of using WPF to use MVVM design pattern. You can read http://msdn.microsoft.com/en-us/magazine/dd419663.aspx for more info on this.

Your DataGrid is bound to the results of the query. With the method you are using, you'll need to requery the database and reset the ItemsSource to the returned results. So following your pattern, you'll need to make the following changes:

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
   RefreshCustomers();
}

private void RefreshCustomers()
{
   using (SIEntities siContext = new SIEntities())
   {
      var query = from p in siContext.Customers
                  select p;

      dataGrid.ItemsSource = query.ToList();
    }
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
   using (SIEntities siContext = new SIEntities())
   {
         Customer cust1 = new Customers();
         cust1.Name = "Pippo";
         cust1.City = "London";
         siContext.Customers.Add(cust1);
         siContext.SaveChanges();
    }

    RefreshCustomers();
}

Upvotes: 3

Related Questions