本文介绍了避免数据网格行在主 - 详细信息场景中选择行时成倍增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用`Linq to Entities`创建的应用程序中有Master-Detail`datagrid`。我想在详细数据网格上显示所选行的主数据网格的详细信息。



我尝试过:



I have Master-Detail `datagrid` in my application created using `Linq to Entities`. I would like to display the selected row''s deatils of the Master datagrid on the details datagrid.

what I have tried :

private void Page_Loaded(object sender, RoutedEventArgs e)
   {
       this.db = new DB_ProdEntities();
       var serverQuery = from a in this.hmdb.Servers
                         orderby a.ServerID
                         select a;
     dgServer.ItemsSource = null;
     dgServer.ItemsSource = serverQuery;
 }
 private void dgServer_SelectionChanged(object sender, SelectionChangedEventArgs e)
   {
       string serverID = (dgServer.SelectedItem as Server).Name;

       var componentQuery = from a in db.Components
                            where a.ServerID == serverID
                            orderby a.Name
                           select a;
     dgComponent.ItemsSource = null;
     dgComponent.ItemsSource = componentQuery;
   }





输出:



当我选择第一行时一切正常,但是当我选择第二行时,它会显示详细信息以及第一行的详细信息选择这样的



如何克服这个问题?



输出1-

[]



输出2

[]

推荐答案

private void Page_Loaded(object sender, RoutedEventArgs e)
        {
if(! IsPostback)
{
            this.db = new DB_ProdEntities();
            var serverQuery = from a in this.hmdb.Servers
                              orderby a.ServerID
                              select a;
          dgServer.ItemsSource = null;
          dgServer.ItemsSource = serverQuery;
}
}
private void dgServer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
     string serverID = (dgServer.SelectedItem as Server).Name;

            var componentQuery = from a in db.Components
                                 where a.ServerID == serverID
                                 orderby a.Name
                                select a;
          dgComponent.ItemsSource = null;
          dgComponent.ItemsSource = componentQuery;
}


这篇关于避免数据网格行在主 - 详细信息场景中选择行时成倍增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 16:08