Hierarchical Data Grid Child
<telerik:RadGridView x:Name="rgvData" CanUserFreezeColumns="False" GridLinesVisibility="Horizontal" IsReadOnly="False" AutoGenerateColumns="False"
telerik:Theming.Theme="Office_Blue" ItemsSource="{Binding}" >
<telerik:RadGridView.ChildTableDefinitions>
<telerik:GridViewTableDefinition />
</telerik:RadGridView.ChildTableDefinitions>
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding StudentID}" Header="Category ID" />
<telerik:GridViewDataColumn DataMemberBinding="{Binding StudentFN}" Header="Category Name" />
<telerik:GridViewDataColumn DataMemberBinding="{Binding StudentLN}" Header="Description" />
<telerik:GridViewDataColumn DataMemberBinding="{Binding Picture}" Header="Picture" />
</telerik:RadGridView.Columns>
<telerik:RadGridView.HierarchyChildTemplate>
<DataTemplate>
<telerik:RadGridView x:Name="RadGridView1" Loaded="rgvData_Loaded" ShowInsertRow="True" CanUserInsertRows="True" ItemsSource="{Binding Subjects}" CanUserFreezeColumns="False" AutoGenerateColumns="False" ShowGroupPanel="False" IsReadOnly="False">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding SubjectID}" Header="Order ID" />
<telerik:GridViewDataColumn DataMemberBinding="{Binding SubjectName}" Header="Employee ID" />
<telerik:GridViewDataColumn DataMemberBinding="{Binding SubjectProfessor}" Header="Order Date" />
<telerik:GridViewDataColumn DataMemberBinding="{Binding ShipCountry}" Header="Ship Country" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</DataTemplate>
</telerik:RadGridView.HierarchyChildTemplate>
</telerik:RadGridView>
MainXaml.Cs
public partial class MainWindow : Window
{
DataAccessDataContext m_dcData;
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
this.rgvData.Items.CollectionChanged += new NotifyCollectionChangedEventHandler(Items_CollectionChanged);
rgvData.Filtered += new EventHandler<Telerik.Windows.Controls.GridView.GridViewFilteredEventArgs>(rgvData_Filtered);
}
void rgvData_Loaded(object sender, RoutedEventArgs e)
{
var childGrid = (RadGridView)sender;
var parentRow = childGrid.ParentRow;
if (parentRow != null)
{
rgvData.SelectedItem = childGrid.DataContext;
parentRow.IsExpandedChanged += new RoutedEventHandler(parentRow_IsExpandedChanged);
}
}
void parentRow_IsExpandedChanged(object sender, RoutedEventArgs e)
{
rgvData.SelectedItem = ((GridViewRow)sender).DataContext;
}x
void rgvData_Filtered(object sender, Telerik.Windows.Controls.GridView.GridViewFilteredEventArgs e)
{
DataContext = m_dcData;
m_dcData = new DataAccessDataContext();
m_dcData.Log = Console.Out;
rgvData.ItemsSource = m_dcData.Students;
rgvData.ShowInsertRow = true;
}
private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
try
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (Student item in e.NewItems)
{
m_dcData.Students.InsertOnSubmit(item);
}
}
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (Student item in e.OldItems)
{
m_dcData.Students.DeleteOnSubmit(item);
}
}
}
catch (Exception s)
{ }
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
m_dcData = new DataAccessDataContext();
m_dcData.Log = Console.Out;
rgvData.ItemsSource = m_dcData.Students;
rgvData.ShowInsertRow = true;
rgvData.ItemsSource = m_dcData.Students;
rgvData.ShowInsertRow = true;
}
}
Hi I have no problem on loading the child data related to its parent source. The problem is that if I try to expand another row it loads the first data for it. Example is if I have row1 expanded it displays the correct child table but if I go to row2 and expand it, its child records are for row1. I tried looking for the answer and putting the parent_IsExpandedChanged开发者_JS百科 event to handle it but still it wont display the correct data. If I try to reload the application again and try to expand row 2 or row 3. its child is correct but if i expanded another row it will display the child for row 2 or row 3. Please help what am i doing wrong
Your XAML looks sound, and I can't see that the code you posted would cause a problem. Have you confirmed that the data loaded is correct? You might actually be loading the same subject data for each student. You can easily check by putting this code into a button click event on your window:
IEnumerable<Student> list = (IEnumerable<Student>)this.DataContext;
foreach(Student stu in Students)
{
Debug.WriteLine(stu.StudentID + ":");
foreach(Subject sub in stu.Subjects)
{
Debug.WriteLine("\\t" + sub.SubjectID)
}
}
精彩评论