0

Ok...as I continue to muddle my way through teaching myself... Here is my latest question. Ha! I have a WPF App using MVVM and three different sample MySQL tables as follows:

BOOKINGS

Booking_ID_Num | Book_Type_Num | Fac_ID_Num |
---------------|---------------|------------|
       1       |       1       |     2      |
       2       |       2       |     3      |
       3       |       1       |     1      |

BOOKING_TYPES

Book_Type_Num |  Book_Type |
--------------|------------|
      1       |  Full Time |
      2       | Singe Case |

FACILITIES

Fac_ID_Num | Facility_Name |
-----------|---------------|
    1      |   Joe's Shop  |
    2      |    MedRX      |
    3      | Grocery Story |

I'm going to try to explain this as succinctly as possible, feel free to criticize... I would like to fill an ObservableCollection and pass that ObservableCollection to a DataGrid. I know how to fill an ObservableCollection through a Model Class, but I don't know how to fill with multiple tables. In this example, I would like to have an Observable Collection in the ViewModel passed to a DataGrid that is structured like this:

Booking_ID_Num | Book_Type_Num |  Book_Type  | Fac_ID_Num | Facility_Name |
---------------|---------------|-------------|------------|---------------|
       1       |       1       |  Full Time  |      2     |     MedRx     |
       2       |       2       | Single Case |      3     | Grocery Store |
       3       |       1       |  Full Time  |      1     |   Joe's Shop  |

Basically, I want to use the Book_Type and Facility_Name in the final solution without having to make any adjustments in the MySQL database like creating a new "view" or something. Does that make sense? This site has been wonderful in helping me learn. Any help on this vague question would be greatly appreciated. Thank you so much!

1 Answer 1

1

Create separate classes that will represent data in your database and data on your DataGrid.

For example:

public class Booking
{
    public int BookingIDNum { get; set; }
    public int BookTypeNum { get; set; }
    public int FacIDNum { get; set; }
}

public class BookingType
{
    public int BookTypeNum { get; set; }
    public string BookType { get; set; }
}

public class Facility
{
    public int FacIDNum { get; set; }
    public string FacilityName { get; set; }
}

public class ViewData
{
    public int BookingIDNum { get; set; }
    public int BookTypeNum { get; set; }
    public string BookType { get; set; }
    public int FacIDNum { get; set; }
    public string FacilityName { get; set; }

    public static ViewData From(Booking booking, BookingType bookingType, Facility facility)
    {
        return new ViewData
        {
            BookingIDNum = booking.BookingIDNum,
            BookingTypeNum = booking.BookingTypeNum,
            FacIDNum = booking.FacIDNum,
            BookType = bookingType.BookType,
            FacilityName = facility.FacilityName
        };
    }
}

on the example above, the classes Booking, BookingType, and Facility represents data in your database while ViewData will be the class you bind on your DataGrid.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok. That somewhat makes sense. So how would I fill each property from the database? Do I put the MySQL data pull method in the "set" of each property or is there something like a "foreach" method I can use with the ViewData class? I apologize for my ignorance. Trying to learn.
See edited answer. ViewData.From method. Does that answer your question?
I'm sorry... I'm so confused and probably way over thinking this... how do I get the data from the MySQL database into the Model Classes "Bookings", "BookingType" and "Facility"? I thought the Model Classes weren't supposed to have any function. No? If not, how do I get the MySQL data into the "ViewData" class? Finally, how do all the different properties relate to each other so I'm not left with 3 or 4 different sets of lists/collections in the columns. IE: when Fac_ID_Num is 1, "Joe's Shop" and not just the 3rd value of Facility_Name?
I'm an idiot... I know. But I'm trying really hard to learn. Ha! :-)
Then, you should implement another class (or classes) that will get data from your database and turn it into Booking, BookingType, and Facility

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.