secret
Last active

SelectedItem of a Datagrid bound to another Datagrid.

  • Download Gist
MainViewModel.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using tryout13022013.PersonViewModels;
using System.ComponentModel;
using tryout13022013.DetailsViewModel;
using tryout13022013.PersonModel;
 
namespace tryout13022013
{
public class MainViewModel
{
private PersonViewModel _subPerson = new PersonViewModel();
public PersonViewModel SubPerson
{
get
{
return _subPerson;
}
set
{
if (_subPerson != value)
{
_subPerson = value; OnPropertyChanged("SubPerson");
}
}
}
 
 
private PersonDetailsViewModel _subDetail = new PersonDetailsViewModel();
public PersonDetailsViewModel SubDetail
{
get { return _subDetail; }
set
{
_subDetail = value; OnPropertyChanged("SubDetail");
}
 
}
 
private Person _selectedPerson;
public Person SelectedPerson
{
get { return _selectedPerson; }
set {
if (_selectedPerson != value)
{
_selectedPerson = value;
OnPropertyChanged("SelectedPerson");
if (this.SelectedPerson != null && this.SubDetail != null)
{
// I dont know how to call the PersonDetailsViewModel class like a method here in order to load its data. kindly help
this.SubDetail.MethodforLoadingPersonDetails(this.SelectedPerson);
}
}
}
 
}
 
 
public event PropertyChangedEventHandler PropertyChanged;
 
private void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
}
}
MainWindow.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<Window x:Class="tryout13022013.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:person="clr-namespace:tryout13022013.PersonViewModels"
xmlns:details="clr-namespace:tryout13022013.DetailsViewModel"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding SubPerson}" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}"
AutoGenerateColumns="False" Height="77" HorizontalAlignment="Left"
Margin="101,26,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="auto" >
<DataGrid.DataContext>
<person:PersonViewModel/>
</DataGrid.DataContext>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Width="auto" Binding="{Binding PersonID}"/>
<DataGridTextColumn Header="First Name" Width="auto" Binding="{Binding FirstName}"/>
<DataGridTextColumn Header="Last Name" Width="auto" Binding="{Binding LastName}"/>
</DataGrid.Columns>
</DataGrid>
 
<DataGrid ItemsSource="{Binding SubDetail}"
AutoGenerateColumns="False" Height="77" HorizontalAlignment="Left"
Margin="101,135,0,0" Name="dataGrid2" VerticalAlignment="Top" Width="255" >
<DataGrid.DataContext>
<details:PersonDetailsViewModel/>
</DataGrid.DataContext>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Width="auto" Binding="{Binding PersonID}"/>
<DataGridTextColumn Header="Address" Width="auto" Binding="{Binding Address}"/>
<DataGridTextColumn Header="Position" Width="auto" Binding="{Binding Description}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Person.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
 
namespace tryout13022013.PersonModel
{
public class Person : INotifyPropertyChanged
{
private int _PersonID;
private string _FirstName;
private string _LastName;
public int PersonID
{
get { return _PersonID; }
set
{
_PersonID = value;
RaisePropertyChanged(this, "Title");
}
}
 
public string FirstName
{
get { return _FirstName; }
set
{
_FirstName = value;
RaisePropertyChanged(this, "FirstName");
}
}
 
public string LastName
{
get { return _LastName; }
set
{
_LastName = value;
RaisePropertyChanged(this, "LastName");
}
}
 
public event PropertyChangedEventHandler PropertyChanged;
 
public void RaisePropertyChanged(object sender, string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
 
}
}
PersonDetails.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
 
namespace tryout13022013.DetailsModel
{
public class PersonDetails : INotifyPropertyChanged
{
private int _PersonID;
public int PersonID
{
get { return _PersonID; }
set
{
_PersonID = value;
RaisePropertyChanged(this, "Title");
}
}
private string address;
public string Address
{
get { return address; }
set
{
address = value;
RaisePropertyChanged(this, "Address");
 
}
}
private string _Description;
public string Description
{
get { return _Description; }
set
{
_Description = value;
RaisePropertyChanged(this, "Description");
}
}
public event PropertyChangedEventHandler PropertyChanged;
 
public void RaisePropertyChanged(object sender, string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
}
}
PersonDetailsViewModel.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.EntityModel;
using tryout13022013.DetailsModel;
using System.Windows.Threading;
using tryout13022013.DetailsModel;
 
namespace tryout13022013.DetailsViewModel
{
public class PersonDetailsViewModel : INotifyPropertyChanged
{
private bool disposed;
private DispatcherTimer timer = new DispatcherTimer();
 
 
Model _myModel = new Model();
private ObservableCollection<PersonDetails> _person = new ObservableCollection<PersonDetails>();
public ObservableCollection<PersonDetails> DetailsData
{
get { return _person; }
set
{
_person = value;
OnPropertyChanged("DetailsData");
}
}
 
public PersonDetailsViewModel()
{
initializeload();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 3);
timer.Start();
}
 
~PersonDetailsViewModel()
{
Dispose(false);
}
 
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
timer.Stop();
timer.Tick -= new EventHandler(timer_Tick);
}
disposed = true;
}
}
 
private void timer_Tick(object sender, EventArgs e)
{
try
{
DetailsData.Clear();
initializeload();
}
catch (Exception ex)
{
timer.Stop();
Console.WriteLine(ex.Message);
 
}
}
 
private void initializeload()
{
try
{
DataTable table = _myModel.getData();
 
for (int i = 0; i < table.Rows.Count; ++i)
DetailsData.Add(new PersonDetails
{
PersonID = Convert.ToInt32(table.Rows[i][0]),
Address = table.Rows[i][1].ToString(),
Description = table.Rows[i][2].ToString()
});
}
 
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
 
public event PropertyChangedEventHandler PropertyChanged;
 
private void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
}
public class Model
{
public DataTable getData()
{
DataTable ndt = new DataTable();
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from [Person].[dbo].[personDetails]", sqlcon);
da.Fill(ndt);
da.Dispose();
sqlcon.Close();
return ndt;
}
}
}
PersonViewModel.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Data.EntityModel;
using System.Windows.Threading;
using tryout13022013.PersonModel;
 
namespace tryout13022013.PersonViewModels
{
public class PersonViewModel : INotifyPropertyChanged
{
private bool disposed;
private DispatcherTimer timer = new DispatcherTimer();
 
 
Model _myModel = new Model();
private ObservableCollection<Person> _person = new ObservableCollection<Person>();
public ObservableCollection<Person> PersonData
{
get { return _person; }
set
{
_person = value;
OnPropertyChanged("PersonData");
}
}
 
public PersonViewModel()
{
initializeload();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 3);
timer.Start();
}
 
~PersonViewModel()
{
Dispose(false);
}
 
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
timer.Stop();
timer.Tick -= new EventHandler(timer_Tick);
}
disposed = true;
}
}
 
private void timer_Tick(object sender, EventArgs e)
{
try
{
PersonData.Clear();
initializeload();
}
catch (Exception ex)
{
timer.Stop();
Console.WriteLine(ex.Message);
 
}
}
 
private void initializeload()
{
try
{
DataTable table = _myModel.getData();
 
for (int i = 0; i < table.Rows.Count; ++i)
PersonData.Add(new Person
{
PersonID = Convert.ToInt32(table.Rows[i][0]),
FirstName = table.Rows[i][1].ToString(),
LastName = table.Rows[i][2].ToString()
});
}
 
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
 
public event PropertyChangedEventHandler PropertyChanged;
 
private void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
}
public class Model
{
public DataTable getData()
{
DataTable ndt = new DataTable();
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from [Person].[dbo].[persons]", sqlcon);
da.Fill(ndt);
da.Dispose();
sqlcon.Close();
return ndt;
}
}
 
}
app.xml
XML
1 2 3 4 5 6 7 8
<!--app.config file-->
 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=User476;Initial Catalog=Person;Persist Security Info=True;User ID=Dbg;Password=x7H8U"/>
</connectionStrings>
</configuration>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.