我正在使用 WPF、DataSet(连接到 SQL Server Express)、XAML 和 C#。 Visual Studio 2013 Express。
我从名为 BankNoteBook 的现有 SQL Server Express 数据库创建了一个名为 BankNoteBookDataSet 的数据集。
在SQL数据库中有一个名为Catalogue的表,为了简单起见,它有2个字段:BNBID和CountryID。
BNBID 是唯一 ID,Country ID 字段与另一个名为 Country 的表具有多对一的关系。 Country 表有 2 个字段:CountryID 和 CountryName。我想在 dataGrid 中显示 BNBID 和 CountryName。
在 VS 的“数据源”窗口中,我将目录的 DataGrid 添加到设计器表面。运行程序,数据网格将显示 BNBID 和 CountryID,但我希望它显示相关国家/地区表中的国家/地区名称。
Ex of Output:
>BNBID Country
0 5
1 3
2 17
3 1
Ex of Desired Output
>BNBID Country
>0 Canada
>1 Australia
>2 United States
现在已经是 2014 年了,这可以通过在 XAML 中绑定来完成吗?或者我是否需要尝试像 2009 年和 2010 年那样的事情:
如何在 WPF 数据网格中显示相关表中的数据 https://stackoverflow.com/questions/3526405/how-to-display-data-from-related-tables-in-a-wpf-datagrid
如何将 WPF Datagrid 绑定到联接表 https://stackoverflow.com/questions/1863621/how-to-bind-wpf-datagrid-to-a-joined-table
WPF XAML - 将数据网格列绑定到外键(数据集) https://stackoverflow.com/questions/19282589/wpf-xaml-bind-datagrid-column-to-foreign-key-dataset
My XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MDGridTest" x:Class="MDGridTest.MainWindow"
Title="MainWindow" Height="980" Width="1024" Loaded="Window_Loaded">
<Window.Resources>
<local:BankNoteBookDataSet x:Key="bankNoteBookDataSet"/>
<CollectionViewSource x:Key="catalogueViewSource" Source="{Binding Catalogue, Source={StaticResource bankNoteBookDataSet}}"/>
</Window.Resources>
<Grid DataContext="{StaticResource catalogueViewSource}">
<DataGrid x:Name="catalogueDataGrid"
RowDetailsVisibilityMode="VisibleWhenSelected" Margin="10,51,10,699"
ItemsSource="{Binding}"
EnableRowVirtualization="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="dbBNBIDColumn" Width="SizeToHeader" IsReadOnly="True" Header="db BNBID" Binding="{Binding dbBNBID}"/>
<DataGridTextColumn x:Name="dbCountryIDColumn" Width="SizeToHeader" Header="db Country ID" Binding="{Binding dbCountryID}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
我在 .CS 文件中自动生成的代码:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MDGridTest.BankNoteBookDataSet bankNoteBookDataSet = ((MDGridTest.BankNoteBookDataSet)(this.FindResource("bankNoteBookDataSet")));
// Load data into the table Catalogue. You can modify this code as needed.
MDGridTest.BankNoteBookDataSetTableAdapters.CatalogueTableAdapter bankNoteBookDataSetCatalogueTableAdapter = new MDGridTest.BankNoteBookDataSetTableAdapters.CatalogueTableAdapter();
bankNoteBookDataSetCatalogueTableAdapter.Fill(bankNoteBookDataSet.Catalogue);
System.Windows.Data.CollectionViewSource catalogueViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("catalogueViewSource")));
catalogueViewSource.View.MoveCurrentToFirst();
}
谢谢你的时间。