Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii ,


I need to populate the multiple nested datagrids

like i have a first datagrid when a row in the first datagrid is clicked or selected below this row a second datagrid with related information from the row selected in the first datagrid will be displayed.
So far i have coded this mu ch.. but my pblm is dat.. the out put i am getting is a empty datagrid...
Below i am writing the coding i have done so far...
Here the output i need is like.. first grid shows a list of colleges and their general details like location,principal,VP etc.. when each row is clicked it should show an other grid below that which displays the branches offered, no of seats, no of projects,staff etc..

I did it using observable collection and MVVM..

plz do help me out in finding the error here..

MainWindow.Xaml
<window x:class="WpfApplication1.Views.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModel="clr-namespace:WpfApplication1.ViewModels"
Title="MainWindow" Height="350" Width="789" FontSize="16">
<window.datacontext>
<viewmodel:class1 x:name="class1" xmlns:viewmodel="#unknown">


<grid>
<Label Content="COLLEGE INFORMATION" Height="37" HorizontalAlignment="Left" Margin="228,35,0,0" Name="label1" VerticalAlignment="Top" Width="268" Foreground="#FFEF0F0F" FontFamily="Comic Sans MS" FontSize="20" />
<datagrid autogeneratecolumns="False" height="200" horizontalalignment="Left" margin="40,78,0,0" name="dataGrid1" verticalalignment="Top" width="683" isenabled="True" isreadonly="True" itemssource="{Binding col}" selectionunit="FullRow">
<datagrid.rowdetailstemplate>
<datatemplate>
<datagrid x:name="Innergrid" autogeneratecolumns="False" itemssource="{Binding ElementName=BranchList,Path=col}" isenabled="True" isreadonly="True" selectionunit="FullRow" width="500">
<datagrid.columns>
<datagridtextcolumn header="ID" binding="{Binding BID}">
<datagridtextcolumn header="Branch" binding="{Binding BranchName}">
<datagridtextcolumn header="Seats" binding="{Binding SeatNo}">
<datagridtextcolumn header="Staff" binding="{Binding Staff}">
<datagridtextcolumn header="Mini Projects" binding="{Binding Minipjct}">
<datagridtextcolumn header="Major Project" binding="{Binding Majorpjct}">




<datagrid.columns>
<datagridtextcolumn binding="{Binding ID}" width="100" header="ID">
<datagridtextcolumn binding="{Binding Names}" width="110" header="College Name">
<datagridtextcolumn binding="{Binding Uni}" width="100" header=" University">
<datagridtextcolumn binding="{Binding Loc}" width="135" header=" College Location">
<datagridtextcolumn binding="{Binding Pri}" width="100" header=" Principal">
<datagridtextcolumn binding="{Binding VP}" width="150" header=" Vise President ">














class1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using WpfApplication1.Models;
//using System.Data.EntityModel;
namespace WpfApplication1.ViewModels
{
public class Class1:INotifyPropertyChanged
{
Model m1=new Model();
private ObservableCollection<college> c1 = new ObservableCollection<college>();
public ObservableCollection<college> col
{
get { return c1; }
set
{
c1=value;
OnPropertyChanged("col");
}
}

public Class1()
{
initializeload();

}
private void initializeload()
{
try
{
DataTable table = m1.getData();
// College college = new College();
for (int i = 0; i < table.Rows.Count; ++i)
{
College college = new College();
college.ID = (int)table.Rows[i][0];
college.Names = table.Rows[i][1].ToString();
college.Uni = table.Rows[i][2].ToString();
college.Loc = table.Rows[i][3].ToString();
college.Pri = table.Rows[i][4].ToString();
college.VP = table.Rows[i][5].ToString();
DataTable table1 = m1.getdata1();
for (int j = 0; j < table1.Rows.Count; ++j)
{
Branch br = new Branch();
br.BID = (int)table1.Rows[j][0];
br.Colid = (int)table1.Rows[j][1];
br.BranchName = table1.Rows[j][2].ToString();
br.SeatNo = table1.Rows[j][3].ToString();
br.Staff = table1.Rows[j][4].ToString();
br.MiniPjct = table1.Rows[j][5].ToString();
br.Majorpjct = table1.Rows[j][6].ToString();
college.BranchList.Add(br);
}
// college.BranchList.Add(br);
col.Add(college);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
}
public class Model
{
public DataTable getData()
{
SqlConnection con = new SqlConnection("Data Source=NESTAERO-014;Initial Catalog=nest1;User ID=sa;Password=nest123@!");
DataTable ndt = new DataTable();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select ID,Name,University,Location,principal,VP from college", con);
da.Fill(ndt);
return ndt;
}
public DataTable getdata1()
{
SqlConnection con = new SqlConnection("Data Source=NESTAERO-014;Initial Catalog=nest1;User ID=sa;Password=nest123@!");
DataTable ndt1 = new DataTable();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select ID,CID,Branchname,SeatNo,staff,Miniproject,Majorpjct from courses cu join college co where cu.ID=co.ID", con);
da.Fill(ndt1);
return ndt1;
}
}

}

college.cs
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;

namespace WpfApplication1.Models
{
public class College : INotifyPropertyChanged
{
public College()
{
}
private int id;
public int ID
{
get { return id; }
set
{
id = value;
OnPropertyChanged("ID");
}
}
private string names;
public string Names
{
get { return names; }
set
{
names = value;
OnPropertyChanged("Names");
}
}
private string uni;
public string Uni
{
get { return uni; }
set
{
uni = value;
OnPropertyChanged("Uni");
}
}
private string loc;
public string Loc
{
get { return loc; }
set
{
loc = value;
OnPropertyChanged("Loc");
}
}
private string pri;
public string Pri
{
get { return pri; }
set
{
pri = value;
OnPropertyChanged("Pri");
}
}
private string vp;
public string VP
{
get { return vp; }
set
{
vp = value;
OnPropertyChanged("VP");
}
}
//Model1 m2 = new Model1();
private List<branch> br = new List<branch>();
public List<branch> BranchList
{
get
{
return br;
}
set
{
br = value;

}
}
public event PropertyChangedEventHandler PropertyChanged;
// private College coll;
protected void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
}

}


Branch.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data.SqlClient;
namespace WpfApplication1.Models
{
public class Branch:INotifyPropertyChanged
{
public Branch()
{
}
public int bid;
public int BID
{
get { return bid; }
set
{
bid = value;
OnPropertyChanged("BID");
}
}
public int colid;
public int Colid
{
get { return colid; }
set
{
colid = value;
OnPropertyChanged("Colid");
}
}
public string brname;
public string BranchName
{
get { return brname; }
set
{
brname = value;
OnPropertyChanged("BranchName");
}
}
public string seat;
public string SeatNo
{
get{return seat;}
set
{
seat=value;
OnPropertyChanged("SeatNo");
}
}
public string staff;
public string Staff
{
get { return staff; }
set
{
staff = value;
OnPropertyChanged("Staff");
}
}
public string mini;
public string MiniPjct
{
get{return mini;}
set{
mini=value;
OnPropertyChanged("Minipjct");
}
}
public string major;
public string Majorpjct
{
get{return major;}
set{
major=value;
OnPropertyChanged("Majorpjct");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyname)
{
var handler= PropertyChanged;
if(handler!=null)
handler(this,new PropertyChangedEventArgs(propertyname));
}

}
}

Mainwindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Collections;

namespace WpfApplication1.Views
{
///
/// Interaction logic for MainWindow.xaml
///


public partial class MainWindow : Window
{
public MainWindow()
{

InitializeComponent();

}

}
}
Posted
Comments
Honey sree 26-Dec-12 2:20am    
Sry.. I am not able to post the xaml code properly here..

1 solution

Hi , I'll try to explain in a simple way (for example departments and persons)

C#
public class Person{
public int Id{get;set;}
public string Name {get;set;}
}

public class Department{
public int Id{get;set;}
public string Name{get;set;}
public ObservableCollection<person> Persons {get;set;}
}

public class MainVM:ViewModelBase
{
  //in properties below do not forget to inject INPC approach
  public Person SelectedPerson {get;set}
  public Department SelectedDepartment {get;set;}
  
  public ObservanleCollection<department> Departments {get;set;}
}</department></person>



your window:
XML
<window ....="" datacontext="{Binding">
<stackpanel>
...
<datagrid x:name="dgDeps" itemssource="{Binding Departments}" xmlns:x="#unknown">
SelectedItem="{Binding SelectedDepartment, Mode=TwoWay}".... />

<datagrid x:name="dgPersons" itemssource="{Binding SelectedDeoartment.Persons}">
SelectedItem="{Binding SelectedPerson, Mode=TwoWay}"
</datagrid></datagrid></stackpanel></window>


whats going on behind the scene, when you populate Departmnets property in MainVM
dgDeps automatically binds to all values and display it,
after you clicked on some department row, dgPersons will refresh bindings according to seleted department.

Hope this will help )
 
Share this answer
 
Comments
Honey sree 26-Dec-12 3:59am    
Thank u for the help... but even after applying wat u have said .. am not getting the output for my application.. Am not able to find out where the pblm is occuring..
Oleksandr Kulchytskyi 26-Dec-12 4:09am    
My answer was concise, so as a result in property declaration i have ommited INPC(INotifyPropertyChanged mechanism) in WPF and MVVM it's need to be persisted anywhere , so without delraring this properties AS INP (in Models and ViemModel) it won't be work properly..
Oleksandr Kulchytskyi 26-Dec-12 5:32am    
And one more , recently one person posted about Master-Detail MVVM in WPF
, so you can read it carefully to understant this approach

http://www.codeproject.com/Articles/332615/WPF-Master-Details-MVVM-Application

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