It would be much easier to do this with bindings. You can add a DetailElements collection to each item in your ItemsGrid1 ItemsSource. Now all you have to do is bind this collection to your ItemsGrid2 'ItemsSource, and it is automatically populated with data through the binding.
public class DataGrid1SourceItem { public ObservableCollection<DetailItem> DetailItems {get;set;} }
XAML:
<Grid> <my:DataGrid Name="dataGrid1" ItemsSource="{Binding}"> <my:DataGrid.RowDetailsTemplate> <DataTemplate> <my:DataGrid Name="dataGrid2" ItemsSource="{Binding Path=DetailItems}"></my:DataGrid> </DataTemplate> </my:DataGrid.RowDetailsTemplate> </my:DataGrid> </Grid>
EDIT: to search your database depending on the value of the DataGrid cells you need to get this value in your ViewModel. To do this, create a property (in my example ProductName
) and bind it to the DataGridColumn Binding property (Mode = TwoWay). Then you can have a private field that contains all the products and filters this field in the ItemsGrid2 ItemsSources data collection:
public class DataGrid1SourceItem { private List<DetailItems> _allDetailItems = new List<DetailItems>(); public IEnumerable<DetailItem> DetailItems { get { return _allDetailItems.Where(item => item.Name == ProductName); } } public DataGrid1SourceItem() {
source share