Search code examples
wpfdata-bindingmodeldatagrid

Change API response Object name (in Model) for Datagrid binding


I'm using WPF and still relatively new. I haven't yet made the jump to MVVM, still trying to wrap my head around it.

I currently have a small app which extracts data from 4 different sources (at different times) and then displays it on a datagrid. The issue is that the data model from the responses differs and I want to be able to easily use the same datagrid.

EG. Datagrid binds to a model [zephyrPatientData] Firstname : binds to firstName last name: binds to lastName

ONE of the extractions has: Firstname = firstName; lastName = SURNAME.

Therefore if I use this data and try and bind to the datagrid, the LASTNAME firle is not filled as that model doesn't have lastName, it has surname.

Below: the datamodel that is working with the datagrid.

public class zephyrPatientData
{
    public int CustomerID { get; set; }
    public int ClaimID { get; set; }
    public string ChemistID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ClaimNumber { get; set; }
    public bool concern { get; set; }
    public int? InsCompID { get; set; }
    public string InsCompName { get; set; }
    public int? EmployerID { get; set; }
    public string EmployerName { get; set; }
    public string DateOfInjury { get; set; }
    public string dateOfBirth { get; set; }
    public string Address { get; set; }
    public int? SuburbID { get; set; }
    public string SuburbName { get; set; }
    public string Postcode { get; set; }
    public string custPhone { get; set; }
    public int? claimTypeID { get; set; }
    public string InvoiceTo { get; set; }
    public string Comments { get; set; }
    public string ClaimManager { get; set; }
    public string Injury { get; set; }
    public bool activeClaim { get; set; }
    public string ClaimManagerEmail { get; set; }
    public int? LawyerID { get; set; }
    public string LawyerCompany { get; set; }
    public double? outstandingScripts { get; set; }
    public double? outstandingValue { get; set; }

    public string dispenseConnected { get; set; }

    public string dispenseID { get; set; }


}

Below: The Datamodel I want to work with the datagrid.

public class zDispensePatientData
    {
        public string id { get; set; }
        public int clientNo { get; set; }
        public string firstName { get; set; }
        private string surname;
        
        public string lastName
        {
            get
            {
                return surname;
            }
            set
            {                
                    surname = value;
            }
        }
        public string title { get; set; }
        public object licenseNo { get; set; }
        public string sex { get; set; }
        public object dateOfBirth { get; set; }
        public object postalAddress { get; set; }
        public object postalSuburb { get; set; }
        public object postalState { get; set; }
        public object postalPostCode { get; set; }
        public string homeAddress { get; set; }
        public string homeSuburb { get; set; }
        public string homeState { get; set; }
        public string homePostCode { get; set; }
        public string phoneNumber { get; set; }
        public string mobileNumber { get; set; }
        public object faxNumber { get; set; }
        public string email { get; set; }
        public DateTime lastModified { get; set; }
        public bool active { get; set; }
        public bool deceased { get; set; }
    }

As you can see...I TRIED to use the get;set; section of the [zDispensePatientData] however I have no idea how to use this correctly. In reality in laman's terms I want it to read surname from the API response but then be accessible as lastName in for use in the Datagrid.

The reaons for this is that there will be another 4 [zDispensePatientData] (other names) and all will have different field names but in the end I need them to work in the same datagrid without changing the binding of every single datagrid column.

EDIT: Expanded the question:

So I appreciate the responses but I'm not exactly grasping how to get this done.

If I were to take away all the model fields and just leave... 3:

  • firstName
  • lastName
  • patientID

my task is to have 5 different models (from responses from API's) 'allocate' to my own model.

eg.

<my model>
public string id { get; set; }
public string lastName { get; set; }
public string firstName { get; set; }

<model API 1>
public string id { get; set; }
public string patientLastName { get; set; }
public string patientFirstName { get; set; }

<model API 2>
public string patientid { get; set; }
public string surname{ get; set; }
public string FIRSTNAME{ get; set; }

Any app will only ever have one connection ([dispenseType]).

So I guess what I need is checking the user dispenseType and then using the model from the API they are using.

Or is it possible to SET myModel based on the contents of the other models.

eg. if API1.firstName = null myModel.firstName = api2.FIRSTNAME

My main issue is I don't know the concept or question to ask. If there is a pointer to the terminology of what I'm trying to achieve I'm happy to go out and learn it to implement it.


Solution

  • How to map properties of two different objects? This question answers my question but was stated in a better way than I could