【问题标题】:Show data from related table in a data grid upon selecting a data grid row in WPF在 WPF 中选择数据网格行后,在数据网格中显示相关表中的数据
【发布时间】:2018-04-24 11:00:46
【问题描述】:

我有两张桌子,设备和组件。每个设备由一个或多个组件组成。现在我有一个显示设备的数据网格,以及来自我数据库中的视图的相关属性。

我想要的是在同一个窗口中有第二个数据网格,它将显示数据网格中所选设备包含的组件。

到目前为止,我知道我可以使用 SelectedItem 属性获取选定的行:

 Equipment eq= (Equipment )myDataGrid.SelectedItem;

但是这段代码应该在什么时候运行呢?我正在使用 EF 将我的数据库实体映射到 CLR 对象,其中我也包含了组件及其关系表。

当用户在设备中选择一行时,我当然需要用新信息刷新组件数据网格,我可以这样做。

 myGrid.ItemsSource = myDataSource;

我该如何着手解决这个问题?

我正在使用一个视图,其中包括来自我的设备数据网格中 3 个不同表的数据,因此设置为数据网格 ItemsSource 的表与组件表没有直接关系。

【问题讨论】:

    标签: c# sql wpf entity-framework datagrid


    【解决方案1】:

    当调用设备数据网格上的 SelectionChanged 事件时,我通过获取组件并将它们插入数据网格来修复它:

    private void EquipDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                try
                {
                    var row_list = GetDataGridRows(EquipDataGrid);
                    foreach (DataGridRow single_row in row_list)
                    {
                        if (single_row.IsSelected == true)
                        {
                            EquipmentView selectedEquipment = (EquipmentView)EquipDataGrid.SelectedItem;
                            using (wiki_nolek_dk_dbEntities db = new wiki_nolek_dk_dbEntities())
                            {
                                db.Configuration.LazyLoadingEnabled = true;
                                var equipmentRelation = db.EquipmentComponents.Where(c => c.EquipmentID == selectedEquipment.EquipmentId);
                                var componentsForEquipment = new List<Component>();
                                foreach (var row in equipmentRelation)
                                {
                                    var component = db.Components.FirstOrDefault(c => c.ComponentId == row.ComponentID);
                                    componentsForEquipment.Add(component);
                                }
                                CompDataGrid.ItemsSource = componentsForEquipment;
                            }
                        }
                    }
    
                }
                catch
                {
                    MessageBox.Show("Det valgte udstyr eksisterer ikke.");
                }
            }
    

    【讨论】:

      【解决方案2】:

      我已经修改了您自己的回复代码以删除无用的foreach 循环。我不知道wiki_nolek_dk_dbEntities 是如何工作的,但我还将ToList() 添加到任何db 查询结果中以确保结果是 List 而不是 IQueryable

              private void EquipDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              try
              {
                  EquipmentView selectedEquipment = (EquipmentView)EquipDataGrid.SelectedItem;
                  using (wiki_nolek_dk_dbEntities db = new wiki_nolek_dk_dbEntities())
                  {
                      db.Configuration.LazyLoadingEnabled = true;
      
                      var equipmentRelationComponentIds =
                          db.EquipmentComponents
                              .Where(e => e.EquipmentID == selectedEquipment.EquipmentId)
                              .Select(e => e.ComponentId)
                              .ToList();
      
                      var componentsForEquipment =
                          db.Components
                              .Where(c => equipmentRelationComponentIds.Contains(c.ComponentId))
                              .ToList();
      
                      CompDataGrid.ItemsSource = componentsForEquipment;
                  }
              }
              catch
              {
                  MessageBox.Show("Det valgte udstyr eksisterer ikke.");
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-15
        • 2013-03-08
        • 2011-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        相关资源
        最近更新 更多