我有以下xaml:
<Window x:Class="level4.Workers"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:level4"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Workers" Height="300" Width="400">
<Window.Resources>
<!--The workers DataBase-->
<ObjectDataProvider x:Key="WorkersDataProvider" ObjectType="{x:Type local:WorkerAdapter}"/>
<ObjectDataProvider x:Key="Workers" ObjectInstance="{StaticResource WorkersDataProvider}" MethodName="getWorkers"/>
<!--The person data provider-->
<ObjectDataProvider x:Key="PersonDataProvider" ObjectType="{x:Type local:PersonAdapter}" />
<ObjectDataProvider x:Key="Person" ObjectInstance="{StaticResource PersonDataProvider}" MethodName="getPersonByID">
<ObjectDataProvider.MethodParameters>
<sys:Int32>0</sys:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource Workers}}">
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" SelectionChanged="WorkersSelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="workers ID" Binding="{Binding WID}" />
<DataGridTextColumn Header="salary (k/m)" Binding="{Binding SALARY}" />
<DataGridTextColumn Header="worker's rank" Binding="{Binding WRANK}" />
<DataGridTextColumn Header="Seniority" Binding="{Binding SENIORITY}" />
<DataGridHyperlinkColumn Header="works in branch:" Binding="{Binding BRANCHNUM}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Grid DataContext="{Binding Source={StaticResource Person}}">
<DataGrid ItemsSource="{Binding Source={StaticResource Person}}" CanUserAddRows="False" IsReadOnly="True" HeadersVisibility="None" AutoGenerateColumns="True" />
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</Window>
函数WorkersSelectionChanged
如下:
private void WorkersSelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid grid = sender as DataGrid;
ObjectDataProvider PersonProvider = this.FindResource("Person") as ObjectDataProvider;
System.Data.DataRowView drv = grid.SelectedItem as System.Data.DataRowView;
PersonProvider.MethodParameters[0] = Int32.Parse(drv["PID"].ToString());
}
函数getPersonByID
如下:
public DataView getPersonByID(Int32 PID)
{
DataRow datarow = dt.Select(String.Format("PID = {0}", PID)).FirstOrDefault();
DataTable ret = new DataTable();
ret.ImportRow(datarow);
return ret.DefaultView;
}
(dt的类型为DataTable)
问题:工作人员表正确显示,但行详细信息始终为空表。我检查了调试器,每次选择一行时调用函数getPersonByID
,使用正确的PID值,DataTable ret
有一些行,但显示的表仍然是空的。
我哪里出错了?
我正在使用VS2012,.NET framework 4.5
答案 0 :(得分:0)
总结评论,您的新DataTable
没有列,因此不使用DataTable.Clone
创建新DataTable
克隆DataTable的结构,包括所有DataTable模式和约束。
所以在你的情况下替换
DataTable ret = new DataTable();
带
DataTable ret = dt.Clone();