To Add/Delete Rows & Update Datasource

Nov 17, 2008 at 1:53 PM
hi there,
just wonder if someone could please help with the following. with the datagrid, when i change the content of a cell and update the datasource, there is no problem, but after i delete/add rows and then try to update the datasource, nothing happens.
i've created a example as follows. the DataContext is created with the O/R Designer. the sql database has a table named products.

<Window x:Class="DataGridTest.Window1"
    Title="Window1" Height="300" Width="300">
  <dg:DataGrid x:Name="dg" AutoGenerateColumns="True">
  <Button x:Name="btnSubmit" Click="btnSubmit_Click">Click</Button>

// cs

namespace DataGridTest
 /// <summary>
 /// Interaction logic for Window1.xaml
 /// </summary>
 public partial class Window1 : Window
  private DataClasses1DataContext dbContext;
  public Window1()
   dbContext = new DataClasses1DataContext();
   dg.ItemsSource = dbContext.products.ToList();

  private void btnSubmit_Click(object sender, RoutedEventArgs e)

Nov 18, 2008 at 6:15 PM
Try changing this line

   dg.ItemsSource = dbContext.products.ToList();

to this

   dg.ItemsSource = dbContext.products;

When you bind to the List, the DataGrid has no way of informing your DataContext when products are added or deleted. However, when you bind to the IQueryable, the DataGrid works some sort of voodoo so that your DataContext is aware of adds and deletes. I don't quite understand what's going on under the hood but it definitely works.

Nov 22, 2008 at 9:10 AM
thanks Peter,

it worked. : )