Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6796841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:32:10+00:00 2026-05-26T18:32:10+00:00

I have a WPF DataGrid that I want to be able to Update, Insert

  • 0

I have a WPF DataGrid that I want to be able to Update, Insert and Delete from. I bind to the DataGrid.DataContext with an ObservableCollection.

When the DataGrid’s SelectionChanged() event fires, I perform a Context.SaveChanges() to write back to the database.

However, the above only works when I Update existing records. When I try to add a new record by clicking on the last row in the DG or when I press delete, the SaveChanges() doesn’t do anything. The data is not added or deleted.

How can I add or delete records from a DataGrid? Here is my DG xaml:

        <DataGrid AutoGenerateColumns="False" Margin="0,12,0,89" Name="grdContact" 
                          CanUserAddRows="True" SelectionMode="Single" IsReadOnly="False"     CanUserDeleteRows="True"
                          ItemsSource="{Binding}"
                          IsSynchronizedWithCurrentItem="True"
                          Focusable="True"
                          SelectionChanged="grdContact_SelectionChanged"

            <DataGrid.Columns>

            <DataGridTextColumn Header="Last Name" Width="150" Binding="{Binding LastName}"/>
            <DataGridTextColumn Header="First Name" Width="150" Binding="{Binding FirstName}"/>
        </DataGrid.Columns>
    </DataGrid>

My Code Behind – Pseudo code:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
            var CustomerObj = new ObservableCollection<Contact>(GetContacts());
            grdContact.DataContext = CustomerObj;
}

    private void grdContact_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Context.SaveChanges();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:32:11+00:00Added an answer on May 26, 2026 at 6:32 pm

    This is because ObservableCollection<T> does not notify the context of added or removed items. In fact, it knows nothing about your database context!

    You’ll have to wire up the CustomerObj collection to add or delete objects from the context:

    CustomerObj.CollectionChanged += CustomerObj_CollectionChanged;
    ...
    
    private void CustomerObj_CollectionChanged(object sender,
        NotifyCollectionChangedEventArgs e)
    {
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Add:
                foreach (var item in e.NewItems.Cast<Contact>())
                    Context.AddObject(item);
                break;
            case NotifyCollectionChangedAction.Remove:
                foreach (var item in e.OldItems.Cast<Contact>())
                    Context.DeleteObject(item);
                break;
            // handle Replace, Move, Reset
        }
    
        Context.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGrid control in a WPF application. I want to be able
I want to achieve the following: Create a WPF DataGrid that have 2 columns:
I have a WPF DataGrid that will be populated from a data table having
I have a WPF DataGrid templatecolumn that has a DataTemplate for an AutoCompleteBox from
I have a strange problem with a WPF DataGrid from WPF Toolkit. The scrollbars
I'm using a the WPF datagrid from the Microsoft CodePlex project. I have a
There is a WPF DataGrid with columns that have checkboxes. These checkboxes are already
I have a wpf datagrid that has it's columns generated dynamically in code and
I have WPF DataGrid (VS2010 C#). I copied the data from DataGrid to Clipboard
I have list of maybe 50,000 entries that are populated in datagrid in wpf.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.