-1

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.

Glenn Angel
  • 381
  • 1
  • 3
  • 14
  • Now I've just tried and changed SURNAME to a public variable and it works.. but is this the correct way to do it? – Glenn Angel Mar 25 '22 at 03:43
  • If I were you, I define an Interface which has the members to be bound and make data models to derive from it. – emoacht Mar 25 '22 at 04:42
  • Thank @emoacht can you explain further? eg. the first model is my list of members that will be bound. is that what you mean? – Glenn Angel Mar 25 '22 at 04:47
  • It's nothing special. Create an Inteface which has only properties which are to be bound, such as `public interface IData { public string FirstName { get; } public string LastName { get; } ... }` and make data models derive from it so that they must have those properties. – emoacht Mar 25 '22 at 07:41

2 Answers2

0

If the column in the DataGrid binds to a property named "LastName", you need to make sure that the type T in the IEnumerable<T> ItemsSource of the DataGrid has such a property.

So add one to the zDispensePatientData class like you have currently done:

public string LastName
{
    get => surname;
    set => surname = value;
}

Another option is to set or bind the ItemsSource to a an IEnumerable<ViewModel> where the ViewModel class has the properties that the DataGrid expects.

You would then translate each zephyrPatientData and/or zDispensePatientData object to a ViewModel, e.g.:

sourceCollection = zDispensePatientDatas
    .Select(x => new ViewModel() { LastName = x.Surname } );
mm8
  • 163,881
  • 10
  • 57
  • 88
0

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

Glenn Angel
  • 381
  • 1
  • 3
  • 14