none
Updating or inserting new records using a queried table in entity framework RRS feed

  • Question

  • Hi,

    (I’m a c# student still. My apology, if this is a stupid question).

    I would like to know if there is a way to edit the data in a datagrid in wpf after its itemsource property is set.

    For example this code is executed and the datagrid displays the results:

    var query = from p in care_Treatment_UnlimitedEntities.Patients

    join pm in care_Treatment_UnlimitedEntities.Patients_Medical_Aids on p.File_Number equals pm.Patient_ID

    join m in care_Treatment_UnlimitedEntities.Medical_Aids on pm.Medical_Aid_ID equals m.Medical_Aid_ID

                 select new {Name = p.Name,med = m.Name};

     

    dataGrid1.ItemsSource = query;

    When I try to double click and edit the information in the datagrid, nothing happens. I know that because of the ‘select new {}’ phrase, it creates anonymous types or something and therefore I cannot edit the entities. But, how else can I select certain columns from multiple tables and edit it based on their relationship? I don’t want the user to edit each table individually and then afterwards make associations between different records.

    Sunday, September 16, 2012 2:33 PM

Answers

  • Hi LLoots;

    I have only dabbled with WPF and I have not been able to put aside enough time to get over the steep learning curve so I will explain how I would do it and leave the coding to you.

    To your question, "How would I go about saving the changes back to the database by using this concrete class.", I would modify the concrete type to store the primary keys for the two fields Name and MED. So I would create the class like this: 

    public class PatientsMedical_Aids
    {
        public string Name { get; set; }
        public string Med { get; set; }
        public int NamePrimaryKey { get; set; }
        public int MedPrimaryKey { get; set; }
    }
    
    // Then in the query change it like this:
    
    var query = (from p in care_Treatment_UnlimitedEntities.Patients
                 join pm in care_Treatment_UnlimitedEntities.Patients_Medical_Aids on p.File_Number equals pm.Patient_ID
                 join m in care_Treatment_UnlimitedEntities.Medical_Aids on pm.Medical_Aid_ID equals m.Medical_Aid_ID
                 select new PatientsMedical_Aids {
                     Name = p.Name, 
                     Med = m.Name,
                     NamePrimaryKey = p.ThePrimaryKeyFieldName,
                     MedPrimaryKey = m.ThePrimaryKeyFieldName
                 }).ToList();

    You will need to hide the two primary keys from being displayed. Then when you detect a change in the data in the DataGrid you can execute a query like this:

    var query = (from p in care_Treatment_UnlimitedEntities.Patients
                 where p.ThePrimaryKeyFieldName == concreteClassInstance.NamePrimaryKey
                 select p).First();
                 
    // Then change the value:
    query.Name = concreteClassInstance.Name;
    
    // Save the changes
    care_Treatment_UnlimitedEntities.SaveChanges();

    // In the above the variable concreteClassInstance it is the one the user clicked in the data grid
    That should give you some ideas on what needs to be done.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by LLoots Sunday, September 16, 2012 8:12 PM
    Sunday, September 16, 2012 7:00 PM
  • Hi LLoots;

    The C# anonymous type are immutable and therefore all its members are read only. Now in order to make the results of the query modifiable you will need to have the query return a concrete type for example in you define a class such as the following class:

    public class PatientsMedical_Aids
    {
        public string Name { get; set; }
        public string Med { get; set; }
    }
    
    //An then midified your query as follows:
    
    var query = (from p in care_Treatment_UnlimitedEntities.Patients
                 join pm in care_Treatment_UnlimitedEntities.Patients_Medical_Aids on p.File_Number equals pm.Patient_ID
                 join m in care_Treatment_UnlimitedEntities.Medical_Aids on pm.Medical_Aid_ID equals m.Medical_Aid_ID
                 select new PatientsMedical_Aids {Name = p.Name, Med = m.Name}).ToList();

    Then the results of the query would be modifiable in local memory. But that in itself will not let you modify the database with the changes. The results of the query are not tracked and to modify a record of the database you need the complete record in local memory.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by LLoots Sunday, September 16, 2012 8:12 PM
    Sunday, September 16, 2012 4:49 PM

All replies

  • Hi LLoots;

    The C# anonymous type are immutable and therefore all its members are read only. Now in order to make the results of the query modifiable you will need to have the query return a concrete type for example in you define a class such as the following class:

    public class PatientsMedical_Aids
    {
        public string Name { get; set; }
        public string Med { get; set; }
    }
    
    //An then midified your query as follows:
    
    var query = (from p in care_Treatment_UnlimitedEntities.Patients
                 join pm in care_Treatment_UnlimitedEntities.Patients_Medical_Aids on p.File_Number equals pm.Patient_ID
                 join m in care_Treatment_UnlimitedEntities.Medical_Aids on pm.Medical_Aid_ID equals m.Medical_Aid_ID
                 select new PatientsMedical_Aids {Name = p.Name, Med = m.Name}).ToList();

    Then the results of the query would be modifiable in local memory. But that in itself will not let you modify the database with the changes. The results of the query are not tracked and to modify a record of the database you need the complete record in local memory.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by LLoots Sunday, September 16, 2012 8:12 PM
    Sunday, September 16, 2012 4:49 PM
  • Thanks for the reply Fernando.<o:p></o:p>

    <o:p> </o:p>

    I can edit the datagrid now, but as you’ve
    said, the changes aren’t tracked. If I may. How would I go about saving the
    changes back to the database by using this concrete class. If I want to update
    a record, would I then use one of the properties in the class to get the data
    or do I have to check the datagrid’s cells and then calling the savechanges()
    method afterwards?<o:p></o:p>


    Sunday, September 16, 2012 5:38 PM
  • Hi LLoots;

    I have only dabbled with WPF and I have not been able to put aside enough time to get over the steep learning curve so I will explain how I would do it and leave the coding to you.

    To your question, "How would I go about saving the changes back to the database by using this concrete class.", I would modify the concrete type to store the primary keys for the two fields Name and MED. So I would create the class like this: 

    public class PatientsMedical_Aids
    {
        public string Name { get; set; }
        public string Med { get; set; }
        public int NamePrimaryKey { get; set; }
        public int MedPrimaryKey { get; set; }
    }
    
    // Then in the query change it like this:
    
    var query = (from p in care_Treatment_UnlimitedEntities.Patients
                 join pm in care_Treatment_UnlimitedEntities.Patients_Medical_Aids on p.File_Number equals pm.Patient_ID
                 join m in care_Treatment_UnlimitedEntities.Medical_Aids on pm.Medical_Aid_ID equals m.Medical_Aid_ID
                 select new PatientsMedical_Aids {
                     Name = p.Name, 
                     Med = m.Name,
                     NamePrimaryKey = p.ThePrimaryKeyFieldName,
                     MedPrimaryKey = m.ThePrimaryKeyFieldName
                 }).ToList();

    You will need to hide the two primary keys from being displayed. Then when you detect a change in the data in the DataGrid you can execute a query like this:

    var query = (from p in care_Treatment_UnlimitedEntities.Patients
                 where p.ThePrimaryKeyFieldName == concreteClassInstance.NamePrimaryKey
                 select p).First();
                 
    // Then change the value:
    query.Name = concreteClassInstance.Name;
    
    // Save the changes
    care_Treatment_UnlimitedEntities.SaveChanges();

    // In the above the variable concreteClassInstance it is the one the user clicked in the data grid
    That should give you some ideas on what needs to be done.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by LLoots Sunday, September 16, 2012 8:12 PM
    Sunday, September 16, 2012 7:00 PM
  • Fernando, thank you very much. I appreciate this. I’ve been struggling to figure this out. This is going to help. Again, thank you.

    Sunday, September 16, 2012 8:17 PM
  •   

    Not a problem, glad to help.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Sunday, September 16, 2012 9:10 PM