浏览Master-Detail Overview并检查该Data Sources
部分以了解您可以实施以提供数据源的具体方式。如果要实现主细节,则必须使用IRelationList
接口或主细节为对象定义关系:使用事件
检查基于Master-Detail 关系中的Pattern 和 Clone Views部分创建的示例
public partial class MasterDetails : Form
{
GridControl grid;
GridView view;
GridView detailView;
GridLevelNode detailNode;
public MasterDetails()
{
InitializeComponent();
InitializeGrid();
InitializeAndAddColumnsToViews();
InitializeAndBindDataSource();
}
private void InitializeGrid()
{
grid = new GridControl();
view = new GridView(grid);
detailView = new GridView(grid);
detailNode = grid.LevelTree.Nodes.Add("SuppliersProducts", detailView);
grid.Dock = DockStyle.Fill;
this.Controls.Add(grid);
}
private void InitializeAndAddColumnsToViews()
{
if (view != null && detailView != null)
{
view.Columns.AddField("CategoryID").VisibleIndex = 0;
detailView.Columns.AddField("ID").VisibleIndex = 0;
detailView.Columns.AddField("Name").VisibleIndex = 1;
detailView.Columns.AddField("Category").VisibleIndex = 2;
}
}
private void InitializeAndBindDataSource()
{
List<BookDetail> bookDetails = new List<BookDetail>();
BookDetail bookDetail = null;
for (int j = 0; j < 5; j++)
{
bookDetail = new BookDetail { CategoryID = j + 1 };
for (int i = 0; i < 5; i++)
{
bookDetail.Books.Add(new Book
{
ID = 1,
Name = "Book - " + (i + 1),
Category = j + 1
});
}
bookDetails.Add(bookDetail);
}
grid.DataSource = bookDetails;
}
}
使用的类:
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
}
public class BookDetail
{
private List<Book> books = new List<Book>();
public int CategoryID { get; set; }
public List<Book> Books
{
get
{
return books;
}
set
{
books = value;
}
}
}
public class Book
{
public int ID { get; set; }
public string Name { get; set; }
public int Category { get; set; }
}
其中 Foos 是主行,并且对于包含 Bars 列表的每个 Foo 显示一个“加号”按钮来显示“Bar”子表?
如果没有成功,请尝试按照上述方法实施,然后按照主 - 详细信息部分的数据规范部分进行操作。
如果您想在单击详细信息展开按钮时获取 Bars,则 crateMaster-Detail: Using Events
它几乎是实现您拥有的任何数据对象的自定义方式。