Devexpress master-detail in 2 gridcontrols

吃可爱长大的小学妹 提交于 2019-12-11 10:45:59

问题


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:


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:

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:

-- 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;


来源:https://stackoverflow.com/questions/35166879/devexpress-master-detail-in-2-gridcontrols

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!