Jean Col
Jean Col

Reputation: 552

How to change the datagrid column name with itemsoure from a query?

Here is my query :

var query = from battery in db.batteries                           
            select battery;

I use this command to load the data in my datagrid :

mydatagrid.ItemsSource = query.ToList();

And the final result is this :

enter image description here

The problem I have is that i cant edit the datagird's columns. For example, i want to hide some columns like the ID column or the ownedid column. I want also to change the columns name : serialnumber -> "Serial number", assemblydate -> "Date".

How can i do that ?

Thanks,

Jean-Baptiste Collet

Upvotes: 0

Views: 383

Answers (2)

Karthik
Karthik

Reputation: 2399

Set autogenerate columns to false and define your own column

<DataGrid x:Name="dgExp" AutoGenerateColumns="False" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="Serial number" Width="*" Binding="{Binding Path=serialnumber }" />
<DataGridTextColumn Header="Date" Width="*" Binding="{Binding Path=assemblydate }" />
</DataGrid.Columns>
</DataGrid>

Upvotes: 2

TrueEddie
TrueEddie

Reputation: 2233

You need to Set AutoGenerateColumns to false and specify your columns in DataGrid.Columns

<DataGrid AutoGenerateColumns="False">
     <DataGrid.Columns>
          <DataGridTextColumn Header="Serial Number" Binding="{Binding serialnumber}" />
          ...
     </DataGrid.Columns>
</DataGrid>

Upvotes: 1

Related Questions