How to detect which item changes and save in Database from Directly DataGrid in C# WPF

Mojtaba_Hakim 321 Reputation points
2021-12-05T22:11:03.713+00:00

I use C# WPF and Entity Framework DB First
I have a Database in SQL Server 2014

I want to save (Insert, Update , Delete) my data in my table but I want to do it directly in DataGrid not using TextBox

36806-gifs.gif

I want to be like Microsoft Excel Data Entry

I tried this in CellEndEdit :

 private void DGR_Formy_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)  
        {  
            if (DGR_Formy.SelectedItem != null && DGR_Formy.SelectedItems != null)  
            {  
                 
                var DtValue = DGR_Formy.ItemContainerGenerator.ItemFromContainer(e.Row);  
  
                var WhatValue = string.Empty;  
      
     
                FrameworkElement element_1 = DGR_Formy.CurrentColumn.GetCellContent(e.Row);  
      
  
                //string selectedColumnHeader = (string)DGR_Formy.SelectedCells[0].Column.Header;  
  
                if (element_1.GetType() == typeof(TextBox))  
                {  
                    WhatValue = ((TextBox)element_1).Text;  
                }  
  
                Telbook LocationTel = (Telbook)DGR_Formy.SelectedItem;  
                 
               
                //Int64 MyID = Convert.ToInt64(dbms.Telbook.Where(t => t.ID.Equals(LocationTel.ID)).FirstOrDefault());  
  
                Telbook telbook = new Telbook()  
                {  
                    ID = LocationTel.ID,  
                    Name = WhatValue,  
                    Tel = LocationTel.Tel  
                };  
                dbms.SaveChanges();  
  
                //dbms.Entry(telbook).State = telbook.ID == 0 ? EntityState.Added : EntityState.Modified;  
            }  
        }  

I know this code is wrong it's just sample What I tried not !

so my problem is I don't know the code

What I need:
In DataGrid Detect What is Changed and Save it (Insert, Update, Delete) in the best performance

Thanks for taking your time to help me

Developer technologies Windows Presentation Foundation
Developer technologies .NET Other
SQL Server Other
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-12-06T00:37:37.663+00:00

    I have a code sample using Entity Framework that saves changes when leaving a cell in a DataGrid.

    WPF/Entity Framework Core primer (C#)

    155105-figure1.png

    1 person found this answer helpful.
    0 comments No comments

  2. Lloyd Sheen 1,486 Reputation points
    2021-12-05T22:42:48.017+00:00

    What you need to do is learn MVVM. If you use that then each cell is bound to a property in a ViewModel. When it changes you get notified in the property set. This is the proper way to do things in WPF.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.