Click here to Skip to main content
15,894,410 members

Selected item for mutilple datagrid's

BuBa1947 asked:

Open original thread
first of all please do not confuse with my question title. It is a single seleteditem on Multiple datagrid ( not multipleselecteditems on a single datagrid).

I have multiple datagrids ( say three ), each datagrid from the lowest depends for its data from the selecteditem from the above datagrid. I have asked questions on selecteditem on datagrid by populating the data from a sqlserver few weeks ago, and i managed to solve this issue with some answers.

WPF SelectedItem binding between two datagrids[^]

is it possible to use the same selecteditem of a datagrid on multiple datagrid's ? for example,in the previous problem it was only 1:1 datagrid, will it be possible to implement the same concept like there are three datagrids,

VB
one - Person

two- PersonDetails

Three- PersonStatus


1. first the Person datagrid loads by default (from the database)

2. Persondetails table data is displayed when a selecteditem is clicked on the Person datagrid(achieved this already)

3. PersonStatus table data should be displayed when the selecteditem is clicked on Persondetails( is this possible, it is like a tree one after the other)

This is how it will look like, I have achieved the selecteditem between the first two datagrid's

http://postimage.org/image/4cpoi2e07/[^]

My MainViewModel.cs

C#
//Datacontext
        public MainViewModel()
        {
            this.Persons = Person.GetPersons();
        }
     
        // for Person Datagrid
        private ObservableCollection<Person> personValues;
        public ObservableCollection<Person> Persons
        {
            get { return personValues; }
            set { this.SetProperty<ObservableCollection<Person>>(ref this.personValues, value); }
        }

       //for the PersonDetails datagrid
        public ObservableCollection<PersonDetails> Details
        {
            get
            {
                if (this.Selectedperson == null)
                {
                    return null;
                }
                return this.LoadDetails(this.Selectedperson.PersonID);
            }

        }
        // method to load the persondetails data
        private ObservableCollection<PersonDetails> LoadDetails(int personID)
        {
            ObservableCollection<PersonDetails> details = new ObservableCollection<PersonDetails>();
            foreach (PersonDetails detail in PersonDetails.GetDetails().Where(item => item.PersonID == personID))
            {
                details.Add(detail);
            }
            return details;
        }

        // SelectedPerson Property
        private Person selectedPersonValue;
        public Person Selectedperson
        {
            get { return selectedPersonValue; }
            set
            {
                this.SetProperty<Person>(ref this.selectedPersonValue, value);
                this.RaiseNotification("Details");
            }
        }

        //for the PersonStatus datagrid
        public ObservableCollection<PersonStatus> Statuses
        {
            get 
            {
                if (this.SelectedDetail == null)
                {
                    return null;
                }
                return this.LoadStatus(this.SelectedDetail.DetailID);
            }
        }

        // method for loading the Status details
        private ObservableCollection<PersonStatus> LoadStatus(int detailID)
        {
            ObservableCollection<PersonStatus> statuss = new ObservableCollection<PersonStatus>();
            foreach (PersonStatus status in PersonStatus.GetStatus().Where(item => item.DetailID == detailID))
            {
                statuss.Add(status);
            }
            return statuss;
        }

        // SelectedDetail Property
        private PersonStatus selectedDetail;
        public PersonStatus SelectedDetail
        {
            get { return selectedDetail; }
            set
            {
                this.SetProperty<PersonStatus>(ref this.selectedDetail, value);
                this.RaiseNotification("Statuses");
            }
        }


XAML

XML
<Grid>
       <DataGrid Margin="100,12,116,219" ItemsSource="{Binding Persons}" SelectedItem="{Binding Selectedperson, Mode=TwoWay}"  />
       <DataGrid Margin="100,110,116,121" ItemsSource="{Binding Details}" SelectedItem="{Binding SelectedDetail,Mode=TwoWay}" />
       <DataGrid ItemsSource="{Binding Statuses}" Margin="100,210,116,21" />
   </Grid>



when I try this code i get this following error :
http://s10.postimage.org/bxk7etjjd/SOerror.png[^]

kindly help to proceed further. Thanks
Tags: C#, Visual Studio, WPF, WPF Toolkit

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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