views:

26

answers:

0

We have a DataSet with an order table and a currency table. The currency id of th order table is linked to the currency table with a relation (see code):

Public Function GetOrders() As DataSet
    Dim dtsOrder As New DataSet
    Dim tblOrder As New DataTable
    Dim tblCurrency As New DataTable

    'Define columns
    tblOrder.Columns.Add("OrderId", System.Type.GetType("System.Int32"))
    tblOrder.Columns.Add("CurrencyId", Type.GetType("System.Int32"))

    tblCurrency.Columns.Add("Id", Type.GetType("System.Int32"))
    tblCurrency.Columns.Add("Name", Type.GetType("System.String"))
    tblCurrency.PrimaryKey = {tblCurrency.Columns(0)}

    'Add data
    tblOrder.Rows.Add(1, 1)
    tblOrder.Rows.Add(2, 1)
    tblOrder.Rows.Add(3, 2)

    tblCurrency.Rows.Add(1, "USD")
    tblCurrency.Rows.Add(2, "EUR")
    tblCurrency.Rows.Add(3, "CHF")

    dtsOrder.Tables.Add(tblOrder)
    dtsOrder.Tables(0).TableName = "Order"
    'Get Customers
    dtsOrder.Tables.Add(tblCurrency)
    dtsOrder.Tables(1).TableName = "Currency"
    dtsOrder.Relations.Add("Order_Currency",
             tblOrder.Columns("CurrencyId"),
             tblCurrency.Columns("Id"),
             False)
    Return (dtsOrder)
End Function

Then we use a datagrid to display the order id, the currency id of the order and the currency name of the related currency table and we have a ComboBox where we can change the currency of the curerently selected order in the datagrid:

Codebehind:

Private Sub MainWindow_Loaded(ByVal sender As Object, 
                              ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            mdtsOrders = GetOrders()
            Me.DataContext = mdtsOrders
            dgrTest.ItemsSource = mdtsOrders.Tables("Order").DefaultView
            With cboCurrency
                .ItemsSource = mdtsOrders.Tables("Currency").DefaultView
                .SelectedValuePath = "Id"   'Currency id
                .DisplayMemberPath = "Name" 'Currency name
            End With
        End Sub


        Private Sub cboCurrency_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cboCurrency.SelectionChanged
                CType(dgrTest.SelectedItem, DataRowView).Row.Item("CurrencyId") = cboCurrency.SelectedValue
End Sub

XAML:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="648" Width="898" WindowState="Normal" WindowStartupLocation="CenterScreen">
    <StackPanel>
        <DataGrid Name="dgrTest"  AutoGenerateColumns="False" Margin="0,0,0,20">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=OrderId}" Header="Order Id"/>
                <DataGridTextColumn Binding="{Binding Path=CurrencyId}" Header="Currency Id" />
                <DataGridTextColumn Binding="{Binding Path=Order_Currency/Name}" Header="Currency Name" Width="10*"/>
            </DataGrid.Columns>
        </DataGrid>
        <ComboBox Name="cboCurrency"  Width="300" Height="25" />
    </StackPanel>
</Window>

The datagrid displays the correct related currency name for every order row. But when we select a row and then change the currency id of the selected order, the currency id is updated in the datagrid but NOT the currency name.

Does anyone have an idea why this does not work? Thanks