Devexpress master-detail in 2 gridcontrols

2019-08-10 12:09发布

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.

1条回答
虎瘦雄心在
2楼-- · 2019-08-10 12:57

I think what you want are two binding sources. Your first binding source, bindingSourceMaster will be bound at design time to Master:

bindingSourceMaster.DataSource = typeof(Master);

Then you can bind your second binding source, bindingSourceSub to the subs property of bindingSourceMaster. The easiest way to do this is at design time like this:

enter image description here

Which will create this code in the .Designer file:

// 
// bindingSourceSub
// 
this.bindingSourceSub.DataSource = this.subsBindingSource;
// 
// subsBindingSource
// 
this.subsBindingSource.DataMember = "subs";
this.subsBindingSource.DataSource = this.bindingSourceMaster;

(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:

List<Master> _MasterList = GetMasterItems();

bindingSourceMaster.DataSource = _MasterList;

Now, when you change the active record in gridControlMaster, you will see that gridControlSubs automatically displays the corresponding detail records for the selected master:

enter image description here

-- EDIT --

Here is my fake data, for what it's worth:

for (int i = 1; i < 100; i++)
{
    Master m = new Master() { id = i, subs = new List<Sub>() };

    for (int j = 1; j < 20; j++)
    {
        Sub s = new Sub() { id = i * 1000 + j, name = Guid.NewGuid().ToString() };
        m.subs.Add(s);
    }

    master.Add(m);
}

bindingSourceMaster.DataSource = master;
查看更多
登录 后发表回答