Hello
I would like to open a window showing the contents of a single record, based on a selection in a datagrid on another window. After double-clicking the appropriate row in the datagrid a new window is opened, passing the key of the selected record:
var newWindow = new ChangeCustomer((Int64)row["Customer_ID"]); newWindow.ShowDialog();
In the new window, the value of the key is passed to a dataprovider method:
C#
public ChangeCustomer(Int64 customer_ID) { InitializeComponent(); ObjectDataProvider customers = this.FindResource("Customers") as ObjectDataProvider; customers.MethodParameters[0] = customer_ID; }
XAML:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApplication1" x:Class="WpfApplication1.EditCustomer" Title="Edit Customer" Height="300" Width="560"><Window.Resources><ObjectDataProvider x:Key="CustomerDataProvider" ObjectType="{x:Type local:CustomerDataProvider}"/><ObjectDataProvider x:Key="Customer" ObjectInstance="{StaticResource CustomerDataProvider}" MethodName="GetViewByKey1"><ObjectDataProvider.MethodParameters><system:Int64>0</system:Int64></ObjectDataProvider.MethodParameters></ObjectDataProvider></Window.Resources><Grid><TextBox HorizontalAlignment="Left" Height="23" Margin="186,41,0,0" TextWrapping="Wrap" Text="{Binding Source={StaticResource Customer}, Path=CustomerName}" VerticalAlignment="Top" Width="120" />
....
</Grid></Window>
Dataprovider method:
public class CustomerDataProvider : IDisposable { private CustomerTableAdapter adapter; public CustomerDataProvider() { LocalDBDataSet dataset = LocalDBDataProvider.ProviderDataSet; adapter = new CustomerTableAdapter(); adapter.Fill(dataset.Customers); } public DataView GetViewByKey1(Int64 primaryKey) { DataView view = LocalDBDataProvider.ProviderDataSet.Customers.DefaultView; if (primaryKey > 0) view.RowFilter = "Customer_ID = " + primaryKey; return view; } // IDisposable lines .....
When I open the ´edit customer´ window for the first time by double clicking on the datagrid, the selected record is shown. But when I close the window, select another row in the datagrid and doubleclick, the window opens with an empty text field.
I can't figure out why. I am sure that the newly selected key returns data because the view in the dataprovider method contains the data of the selected record, but why is that record not shown in the edit customer window?
Thanks.