How Can I Bind Data To DataGridView With EntityFramework In WPF

c# data-binding entity-framework-6 wpf wpf-controls


i have used Store Stored Procedure in my database (SQL Server)

select namebuffet,namefamilymember,idmember,Sum(finalpricefactorbuffet)-Sum((delivery*30)/100) as Price from Buffet_factorbuffet
 Inner join Buffet_buffet
 on idbuffet=buffetidfactor
 Inner join Member_member
 on idmember=memberid
 inner join Buffet_banktransaction
 on factorid=idfactorbuffet
 where success=1 And checkout!=2 And datecheckouted is null
 GROUP BY namebuffet,namefamilymember,idmember

Then i imported this in my WPF project with entity framework and also wrote my class like this

    public class Buffetinformation
        public List<Windows_BuffetInformation_Result> Getbuffetinformation ()
            using (MYDBEntities DB = new MYDBEntities())

                    var q = DB.Windows_BuffetInformation().AsQueryable();
                    return q.ToList();

how can i bind some parts to datagridview controller using above class ,my stored procedure return some columns like (namebuffet,nameandfamilly,idmember and ....) but i want to use only namebuffet and idmember and show them in my datagridview or another controllers

i have seen some examples using but i cant find examples of entity framework

12/10/2018 1:30:19 PM

Accepted Answer

To bind data to DataGrid you just bind your resulting list to DataGrid ItemsSource property. Showing only some parts is mostly about setting up datagrid in XAML. Firstly, in definition of your DataGrid you should set AutoGenerateColumns to False. Then you should define a DataGrid.Columns and make an explicit definition for each column. Something like this:

<DataGrid Name=MyDataGrid AutoGenerateColumns=False>
        <DataGridTextColumn Header="Id" Binding="{Binding idmember}"/>
        <DataGridTextColumn Header="Name" Binding="{Binding namebuffet}"/>
12/10/2018 11:38:46 PM

Popular Answer

You could potentially bind directly to your model objects. Bind the itemssource of a datagrid to a public property Observablecollection in a viewmodel which implements inotifypropertychanged.

An observablecollection can take a List in it's constructor. You have a List there from your ToList(). So you could use that to set your property to a new observablecollection from that list you generate.

That would be a very basic approach because you have nowhere to put logic. More usual would be to iterate the model instances and new up a row viewmodel, copy the properties across. I usually use reflection to copy matching properties.

But if you just want a "get you started".

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow