2

I am developing a User Interface to view the student details from a student Database. I have three datagrids in my application as follows:

The first datagrid at the left is for selecting the Student from the list and the other two must display the information about the selected student.

the Student Info datagrid is dependent on Select a Student datagrid and the Personal Information datagrid is dependent on Student Info datagrid. I have done the database connectivity to populate the list of students in the Select a Student datagrid, but I dont know how to proceed further. So far I have understood that I should create a DataGridView.CellClick Event.

Could anybody suggest me to proceed further.

User Interface for Student database

Arash
  • 3,013
  • 10
  • 52
  • 74
Indhi
  • 1,684
  • 5
  • 27
  • 48

3 Answers3

2

You can bind the ItemsSource of the Personal Information datagrid to the SelectedItem of the Student Info datagrid and the ItemsSource of the Student Info datagrid to the SelectedItem of the Select a Student datagrid.

That way the Student Info datagrid will automatically update as you select a student and the Student Info datagrid will automatically update as you select an item in the Student Info datagrid.

The Binding:

ItemsSource="{Binding ElementName=StudentDataGrid, Path=SelectedItem.StudentInfo}"

Replace the ElementName and the Path with the name of your datagrids and the correct property.

Eirik
  • 4,135
  • 27
  • 29
  • Thank u Eirik, I got a good idea from your suggestion. but what do u mean in the second sentence.. ? "That way the Student Info datagrid will automatically update as you select a student and the Student Info datagrid will automatically update as you select an item in the Student Info datagrid" – Indhi Feb 04 '13 at 14:24
  • 1
    @Buba1947 When you select a student in your first datagrid, the Student Info datagrid will be updated with the correct values since it's bound to the `SelectedItem` of the first datagrid. Same goes for the Personal Information datagrid. – Eirik Feb 05 '13 at 08:29
1

Hi If you following MVVM then its better you bind SelectedItem /SelectedValue of DataGrid to get the SelectedItem instead of SelectionChanged event.I hope this will help.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <ListBox ItemsSource="{Binding Students}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedStudent, Mode=TwoWay}"/>
    <StackPanel Grid.Column="1">
        <TextBlock Text="{Binding SelectedStudent.FamilyName}"/>
        <TextBlock Text="{Binding SelectedStudent.PhoneNumber}"/>
    </StackPanel>
</Grid>

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        Students = new ObservableCollection<Student>();
        Students.Add(new Student()
        {
            Name = "James",
            FamilyName = "mangol",
            PhoneNumber = "01234 111111"
        });
        Students.Add(new Student()
        {
            Name = "Bob",
            FamilyName = "angol",
            PhoneNumber = "01234 222222"
        });
        Students.Add(new Student()
        {
            Name = "Emma",
            FamilyName = "pangol",
            PhoneNumber = "01234 333333"
        });
    }
    public ObservableCollection<Student> Students { get; set; }

    private Student selectedStudent;
    public Student SelectedStudent
    {
        get { return selectedStudent; }
        set { selectedStudent = value; Notify("SelectedStudent"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

}
public class Student:INotifyPropertyChanged
{

    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; Notify("Name"); }
    }



    private string familyname;

    public string FamilyName
    {
        get { return familyname; }
        set { familyname = value;Notify("FamilyName"); }
    }



    private string phonenumber;

    public string PhoneNumber
    {
        get { return phonenumber; }
        set { phonenumber = value; Notify("PhoneNumber"); }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

The above example demonstrates how to use SelectedItem using Binding.This is not the exact solution of your problem but this will give you idea how to use SelectedItem.I have shown for ListBox it works same for DataGrid.

yo chauhan
  • 12,079
  • 4
  • 39
  • 58
0

On selection of a student, you should get the ID of the selected record. And from there, you can load the details based on the ID of the record.

whastupduck
  • 1,156
  • 11
  • 25