c# - Binding a DataGrid -
i've created code-first c# project entity framework , wpf. have created entity named personel entity. i'm dragging , dropping entity mainwindow doesn't show data. think have in mainwindow.xaml.cs file don't know do. here datagrid code in xaml:
<datagrid x:name="personelentitydatagrid" autogeneratecolumns="false" enablerowvirtualization="true" itemssource="{binding}" margin="19,259,18,10" rowdetailsvisibilitymode="visiblewhenselected"> <datagrid.columns> <datagridtextcolumn x:name="addresscolumn" binding="{binding address}" header="address" width="sizetoheader"/> <datagridtextcolumn x:name="agecolumn" binding="{binding age}" header="age" width="sizetoheader"/> <datagridtextcolumn x:name="idcolumn" binding="{binding id}" header="id" width="sizetoheader"/> <datagridtextcolumn x:name="namecolumn" binding="{binding name}" header="name" width="sizetoheader"/> <datagridtextcolumn x:name="phonenumbercolumn" binding="{binding phonenumber}" header="phone number" width="sizetoheader"/> </datagrid.columns> </datagrid>
here code in mainwindow.xaml.cs file :
public partial class mainwindow : window { private personelcontext _context = new personelcontext(); public mainwindow() { initializecomponent(); } private void window_loaded(object sender, routedeventargs e) { system.windows.data.collectionviewsource personelentityviewsource = ((system.windows.data.collectionviewsource)(this.findresource("personelentityviewsource"))); } }
here code in personelcontext.cs file :
namespace personel { public class personelcontext : dbcontext { public dbset<personelentity> personels { get; set; } } }
there nothing else datagrid in code. know need add don't know add. can tell me do?
make 1 change in xaml below
itemsource= {binding} itemsource= {binding path=.}
and in code behind
personelentitydatagrid.itemsource =_context.personels.tolist();
(if above not works try or part).
or
personelentitydatagrid.datacontext =_context.personels.tolist();
Comments
Post a Comment