Display only specific columns in a DataGrid from an Entity Object

I am trying to populate a DataGrid with a SQL query in the Entity model created in VS2010.

public List<MovieTable> LoadMoviesMethod() { ObjectQuery<MovieTable> _movies = dataEntities.MovieTables; var query = from MovieTable in _movies //where MovieTable.Rating == "R" //orderby MovieTable.id select MovieTable; return query.ToList(); } 

A MovieTable is automatically generated when I import my database, but when it appears in the grid, it shows more information than I would like (id, EntityKey and EntityState). Trying to select specific objects in an object, I get the rows back, and the return statement complains. Is there a way to select specific MovieTable participants to display on a datagrid? Maybe specify the columns that I would like to display? It seems pretty simple, but I think I'm not good enough to figure it out !!!!

+6
source share
1 answer

You need to specify AutoGenerateColumns = "False", and then specify the desired columns. Sort of

  <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="Id" Binding="{Binding Id}"/> <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> ... etc 

There are several different types of columns: text, combo box, check box, hyperlink, template ...

This blog comment may be helpful.

+14
source

Source: https://habr.com/ru/post/909207/


All Articles