How do I display the master-detail view in 2 grids instead of one grid. Here's how I populate the grid currently and it does show master-detail view.
I don't know how to set the relation or DataMember property (as shown in some examples that use database) in case of using 2 grid controls to create a relation with the current data structure.
public class Master
{
public int id { get; set; }
public List<Sub> subs { get; set; }
}
public class Sub
{
public int id { get; set; }
public string name { get; set; }
}
//filling some data for master and sub objects
private void FillData()
{
master = new List<Master>();
for (int i = 0; i < 10; i++)
{
Master tmpmaster = new Master();
tmpmaster.id = i;
tmpmaster.name = "Master " + (i + 1).ToString();
tmpmaster.subs = new List<Sub>();
for(int j = 0; j < 5; j++)
{
Sub tmpsub = new Sub();
tmpsub.id = j;
tmpsub.name = "Sub " + (j + 1).ToString();
tmpmaster.subs.Add(tmpsub);
}
master.Add(tmpmaster);
}
}
FillData();
grid = new GridControl();
this.Controls.Add(grid);
grid.DataSource = master;
Thanks for any suggestions.
I think what you want are two binding sources. Your first binding source,
bindingSourceMaster
will be bound at design time toMaster
:Then you can bind your second binding source,
bindingSourceSub
to thesubs
property ofbindingSourceMaster
. The easiest way to do this is at design time like this:Which will create this code in the .Designer file:
(but don't worry about that -- let the designer do the heavy lifting)
gridControlMaster's datasource will be bindingSourceMaster, and gridControlSubs's datasource will be bindingSourceSubs.
From there, .NET and Dev Express will do all of the heavy lifting for you. Once you assign your object to bindingSourceMaster, everything else will work as expected:
Now, when you change the active record in
gridControlMaster
, you will see thatgridControlSubs
automatically displays the corresponding detail records for the selected master:-- EDIT --
Here is my fake data, for what it's worth: