Click here to Skip to main content
15,117,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a classic Ado.net entity like Clients and Orders. Also I have a WPF window and using Mvvm architecture. I am new to mvvm and Ado.net so I cant figure out how can get my datagrid(that using one itemsource) show items from clients table and orders table. Its might sound obvious but problem is taht my Orders property(that i set as ItemSource for datagrid) dont have items from clients table.

My methods to get data from db:
Orders:
XML
public IEnumerable<Order> GetOrders()
        {
            using (var context = new SvLaserEntities())
            {
                var result = context.Orders.ToList();
                result.ForEach(e => context.Detach(e));
                return result;
            }
        }

Clients:
XML
public IEnumerable<Client> GetClients()
        {
            using (var context = new SvLaserEntities())
            {
                var result = context.Clients.ToList();
                result.ForEach(e => context.Detach(e));
                return result;
            }
        }
Posted
Updated 20-Apr-13 11:15am
v2
Comments
[no name] 20-Apr-13 15:50pm
   
How could we correct your code when we cannot possibly see it?
Alex Define 20-Apr-13 16:16pm
   
Well, I thought about an algorithm or advice, but added some code;
[no name] 20-Apr-13 17:15pm
   
Well since you did not provide any relevant information for whatever your problem is, the only advice is could offer is; get the orders from the orders table and then assign the result to your Orders property.

1 solution

Are the two tables connected in your database at all? If not, you likely have to redesign your database slightly. Typically, an Orders table would have a ClientId of some sort, which is a primary key of the Clients table and a foreign key in the Orders table. If you did that, then you could pull the Clients data from the Client table and the relevant Orders using something like:

C#
foreach(Client c in context.Clients)
{
    // do something with c...
    Order o=c.Orders;
    // do something with o...
}


Best of luck!
   
Comments
Alex Define 21-Apr-13 3:01am
   
Thank you! Works great :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900