WPF/Dataset: How do you XAML bind data from a related table into a datagrid column?

人盡茶涼 提交于 2020-01-23 15:53:45

问题


I am working with WPF, DataSet (connection is to SQL Server Express), XAML and C#. Visual Studio 2013 Express.

I have created a DataSet called BankNoteBookDataSet from an existing SQL Server Express database named BankNoteBook.
In the SQL database there is a table named Catalogue, and to keep it simple here, it has 2 fields: BNBID and CountryID.
BNBID is the unique ID and Country ID field has a many to one relationship with another table named Country. The Country table has 2 fields: CountryID and CountryName. I want to display BNBID and CountryName in the dataGrid.

In VS in the Data Sources window I drug a DataGrid of Catalogue to the designer surface. Running the program the datagrid will display the BNBID and CountryID, but I want it to display the CountryName from the related Country table.

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  

Can this be done by binding in XAML now that its 2014? Or do I need to try something like back in 2009 and 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)

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>

My Auto Generated Code in the .CS file:

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();
        }

Thanks for your time.

来源:https://stackoverflow.com/questions/27577970/wpf-dataset-how-do-you-xaml-bind-data-from-a-related-table-into-a-datagrid-colu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!