我正在使用WPF,DataSet(连接到SQL Server Express),XAML和C#。 Visual Studio 2013 Express。
我从名为BankNoteBook的现有SQL Server Express数据库创建了名为BankNoteBookDataSet的DataSet。
在SQL数据库中有一个名为Catalog的表,为了使其简单,它有2个字段:BNBID和CountryID。
BNBID是唯一ID,Country ID字段与另一个名为Country的表具有多对一关系。 Country表有2个字段:CountryID和CountryName。我想在dataGrid中显示BNBID和CountryName。
在Data Sources窗口的VS中,我将Catalog的DataGrid打包到设计器表面。运行程序datagrid将显示BNBID和CountryID,但我希望它显示相关Country表中的CountryName。
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
这可以通过XAML绑定来实现吗?或者我需要在2009年和2010年尝试类似的东西:
How to display data from related tables in a WPF datagrid
How to bind WPF Datagrid to a joined table
WPF XAML - Bind datagrid column to foreign key (Dataset)
我的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();
}
感谢您的时间。