[英]how to show the foreign key value in a datagrid (linq to sql - wpf)
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. 我的应用程序中有一个简单的datagrid,我想用其中的一个表填充它。除ForeignKey列显示MyAppName.tableName而不是其他表中的值(PrimaryKeys)之外,其他所有功能都很好。
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 : Linq查询:
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 谢谢
I'm suspecting that the problem is with the auto generate columns of DataGrid
. 我怀疑问题出在
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). 它遍历
tbl_Airplanes
属性,并调用ToString
表示外键关系(由自定义类表示,而不是已知类型)。
this is why you get MyAppName.tableName
as value for your foreign key 这就是为什么您将
MyAppName.tableName
作为外键值的原因
First approach: You can solve this by customizing DataGrid
columns: 第一种方法:您可以通过自定义
DataGrid
列来解决此问题:
first, add to your DataGrid
xaml elemnt, AutoGenerateColums
to False
(to disable the auto generated behavior) 首先,将
AutoGenerateColums
添加到您的DataGrid
xaml元素中,将其AutoGenerateColums
为False
(以禁用自动生成的行为)
<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.Colums
内部创建并用DataGridTextColumn
或DataGridTemplateColumn
或DataGridCheckBoxColumn
等表示您想要的每个列( 在此处阅读更多 )
<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
): 结果(根据您的
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) 第二种方法:自定义“外键类”的
ToString
(我推荐第一种方法)
in your generated classes, go to your "foreign key class" add custom ToString
在生成的类中,转到“外键类”,添加自定义
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 这样,当自动生成列调用
ToString
,它将获得ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.