Click here to Skip to main content
15,898,938 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 :

C#
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;
   }


OUTPUT:

when I select the first row everything is normal, but when I select the second row it displays the details along with the selection of the first row's details like this

how to overcome this problem?

Output 1-
http://postimg.org/image/9x1f616n7/[^]

output 2
http://s16.postimg.org/4nylei9ph/op2.png[^]
Posted

1 solution

Please try this code


C#
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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900