2个gridcontrols中的Devexpress主从细节 [英] Devexpress master-detail in 2 gridcontrols
问题描述
如何在2个网格而不是一个网格中显示主从视图。
这是我当前填充网格的方式,它确实显示了主从视图。
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.
我不知道如何设置关系或DataMember属性(如在使用2个网格控件创建与当前数据结构的关系的情况下显示在某些使用数据库的示例中。
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;
感谢任何建议。
推荐答案
我认为您想要的是两个绑定源。您的第一个绑定源 bindingSourceMaster
将在设计时绑定到 Master
:
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);
然后,您可以绑定第二个绑定源, bindingSourceSub
到 bindingSourceMaster
的 subs
属性。最简单的方法是在设计时像这样:
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:
将在.Designer文件中创建以下代码:
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的数据源将是bindingSourceMaster,gridControlSubs的数据源将是bindingSourceSubs。
gridControlMaster's datasource will be bindingSourceMaster, and gridControlSubs's datasource will be bindingSourceSubs.
从那里开始,.NET和Dev Express将所有的繁重工作都为您服务。将对象分配给bindingSourceMaster后,其他所有内容都将按预期工作:
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;
现在,当您更改 gridControlMaster $ c $中的活动记录时c>,您会看到
gridControlSubs
自动显示所选主数据库的相应详细记录:
Now, when you change the active record in gridControlMaster
, you will see that gridControlSubs
automatically displays the corresponding detail records for the selected master:
-编辑-
这是我的假数据,为它的价值:
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;
这篇关于2个gridcontrols中的Devexpress主从细节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!