Question

I have a simple datagrid in my app that I want to fill it with one of my tables, Everything works great except the ForeignKey columns show the MyAppName.tableName instead of values (PrimaryKeys)from other tables.

How can I show the values in foreign key columns?

Here is a piece of my code :

<Grid>
    <DataGrid x:Name="DataGrid1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="322" Width="704"/>
</Grid> 

Linq Query :

 DataClasses1DataContext db = new DataClasses1DataContext();
        public void doSimpleLinqQuery()
        {
            var result = from a in db.tbl_Airplanes
                         where a.airplane_model == "t420"
                         select a;

            DataGrid1.ItemsSource = result;
        }

Thanks

Était-ce utile?

La solution

I'm suspecting that the problem is with the auto generate columns of DataGrid .

it iterates over the tbl_Airplanes Properties and calls ToString to represent the foreign key relationship (which is represented by a custom class and not a known type).

this is why you get MyAppName.tableName as value for your foreign key


First approach: You can solve this by customizing DataGrid columns:

first, add to your DataGrid xaml elemnt, AutoGenerateColums to False (to disable the auto generated behavior)

<DataGrid ... AutoGenerateColumns="False">

afterwards, create inside DataGrid.Colums and represent each column you would like with DataGridTextColumn or DataGridTemplateColumn or DataGridCheckBoxColumn etc... (read more here)

<DataGrid.Columns>

    <DataGridTextColumn Header="Name" Binding="{Binding Name}" />

    ...

    <DataGridTextColumn Header="Plane Id" Binding="{Binding Plane.Id}" />

</DataGrid.Columns>

result (customize this according to your tbl_Airplanes):

<DataGrid x:Name="DataGrid1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="322" Width="704" AutoGenerateColumns="False">

    <DataGrid.Columns>

        <DataGridTextColumn Header="Name" Binding="{Binding Name}" />

        <DataGridTemplateColumn Header="Date Added">
                <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                                <DatePicker SelectedDate="{Binding DateAdded}" BorderThickness="0" />
                        </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>


        <DataGridTextColumn Header="Mass" Binding="{Binding Mass}" />

        <DataGridTextColumn Header="Plane Id" Binding="{Binding Plane.Id}" />

    </DataGrid.Columns>

</DataGrid>

Second Approach: customize ToString of "foreign key class" (i recommend the first way)

in your generated classes, go to your "foreign key class" add custom ToString

class Plane
{
    int Id;

    public override string ToString()
    {
        return Id.ToString();
    }
}

this way, when the auto generate columns calls ToString it will get the id

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top