DevExpress GridControl 在 Master-Detail 模式下的问题
Trouble with DevExpress GridControl in Master-Detail mode
我有一个 WinForms 应用程序,里面只有一个 DevExpress GridControl。
此 GridControl 在主从模式下使用两个 GridView 和一个关系。
作为 gridControl 的数据源,我使用以下 class:
public class DashboardParameter
{
public string Name { get; set; }
public int DataType { get; set; }
public int ValueType { get; set; }
public BindingList<DashboardParameterValue> Detail { get; set; }
public DashboardParameter()
{
Detail = new BindingList<DashboardParameterValue>();
}
}
public class DashboardParameterValue
{
public string Value { get; set; }
}
这里是数据加载的代码:
private void MasterDetail_Load(object sender, EventArgs e)
{
data = new BindingList<DashboardParameter>();
var p1 = new DashboardParameter() { Name = "First", DataType = 1, ValueType = 1};
p1.Detail.Add(new DashboardParameterValue() { Value = "Value1" });
p1.Detail.Add(new DashboardParameterValue() { Value = "Value2" });
var p2 = new DashboardParameter() { Name = "Second", DataType = 1, ValueType = 1 };
p2.Detail.Add(new DashboardParameterValue() { Value = "Value3" });
p2.Detail.Add(new DashboardParameterValue() { Value = "Value4" });
data.Add(p1);
data.Add(p2);
gridControl.DataSource = data;
}
据我了解,通过这种方式,我的 gridControl 会自动查找主从关系并为数据源 class 中的每个字段创建列(如果 AutoPopulateColumns 属性 为 true ).
问题: 我无法更改 detailView 列中的任何内容。我不知道我的 dataView 列是在什么时候创建的。所有 detailView 属性都被忽略。
例如,如果我更改 detailView.AutoPopulateColumn = false,列仍在创建中。
如果我创建自定义 GridColumn gridColumn1 并在其中添加 detailView.Columns.Add(gridColumn1),它将被忽略。
我唯一能做的就是使用 [DisplayAttribute] 来更改 DisplayName、Visible 等等。
问题:我必须如何更改我的代码才能更改我的 detailView。
例如,我可以在所有自动生成的列之后在 detailView 中添加 Column,或者将 Column 类型更改为 ComboBox(使用 RepositoryItemComboBox)。
您可以在GridView.MasterRowExpanded event handler. This event is raised when a master row is expanded and a corresponding detail view is created and configured. To access this view, use the GridView.GetDetailView method. Alternatively, you can handle the GridControl.ViewRegistered活动中自定义详细视图。
另一种解决方案是创建一个 pattern detail view 并在运行时或设计时自定义它。
我建议您阅读 Working with Master-Detail Relationships in Code and Master-Detail Relationships 的文档。
您可以根据数据库结构创建视图,也可以通过添加视图和列及其设置来实用地创建视图。
您可以在 GridView.MasterRowExpanded event handler. Fires immediately after a particular detail clone 变得可见时自定义详细视图。 MasterRowExpanded 事件在扩展主行或在详细信息之间切换时触发。
示例:
//Assign a CardView to the relationship
CardView cardView1 = new CardView(gridControl1);
gridControl1.LevelTree.Nodes.Add("CategoriesProducts", cardView1);
//Specify text to be displayed within detail tabs.
cardView1.ViewCaption = "Category Products";
//Hide the CategoryID column for the master View
gridView1.Columns["CategoryID"].VisibleIndex = -1;
//Present data in the Picture column as Images
RepositoryItemPictureEdit riPictureEdit = gridControl1.RepositoryItems.Add("PictureEdit") as RepositoryItemPictureEdit;
gridView1.Columns["Picture"].ColumnEdit = riPictureEdit;
//Stretch images within cells.
我有一个 WinForms 应用程序,里面只有一个 DevExpress GridControl。 此 GridControl 在主从模式下使用两个 GridView 和一个关系。
作为 gridControl 的数据源,我使用以下 class:
public class DashboardParameter
{
public string Name { get; set; }
public int DataType { get; set; }
public int ValueType { get; set; }
public BindingList<DashboardParameterValue> Detail { get; set; }
public DashboardParameter()
{
Detail = new BindingList<DashboardParameterValue>();
}
}
public class DashboardParameterValue
{
public string Value { get; set; }
}
这里是数据加载的代码:
private void MasterDetail_Load(object sender, EventArgs e)
{
data = new BindingList<DashboardParameter>();
var p1 = new DashboardParameter() { Name = "First", DataType = 1, ValueType = 1};
p1.Detail.Add(new DashboardParameterValue() { Value = "Value1" });
p1.Detail.Add(new DashboardParameterValue() { Value = "Value2" });
var p2 = new DashboardParameter() { Name = "Second", DataType = 1, ValueType = 1 };
p2.Detail.Add(new DashboardParameterValue() { Value = "Value3" });
p2.Detail.Add(new DashboardParameterValue() { Value = "Value4" });
data.Add(p1);
data.Add(p2);
gridControl.DataSource = data;
}
据我了解,通过这种方式,我的 gridControl 会自动查找主从关系并为数据源 class 中的每个字段创建列(如果 AutoPopulateColumns 属性 为 true ).
问题: 我无法更改 detailView 列中的任何内容。我不知道我的 dataView 列是在什么时候创建的。所有 detailView 属性都被忽略。
例如,如果我更改 detailView.AutoPopulateColumn = false,列仍在创建中。
如果我创建自定义 GridColumn gridColumn1 并在其中添加 detailView.Columns.Add(gridColumn1),它将被忽略。
我唯一能做的就是使用 [DisplayAttribute] 来更改 DisplayName、Visible 等等。
问题:我必须如何更改我的代码才能更改我的 detailView。
例如,我可以在所有自动生成的列之后在 detailView 中添加 Column,或者将 Column 类型更改为 ComboBox(使用 RepositoryItemComboBox)。
您可以在GridView.MasterRowExpanded event handler. This event is raised when a master row is expanded and a corresponding detail view is created and configured. To access this view, use the GridView.GetDetailView method. Alternatively, you can handle the GridControl.ViewRegistered活动中自定义详细视图。
另一种解决方案是创建一个 pattern detail view 并在运行时或设计时自定义它。
我建议您阅读 Working with Master-Detail Relationships in Code and Master-Detail Relationships 的文档。
您可以根据数据库结构创建视图,也可以通过添加视图和列及其设置来实用地创建视图。
您可以在 GridView.MasterRowExpanded event handler. Fires immediately after a particular detail clone 变得可见时自定义详细视图。 MasterRowExpanded 事件在扩展主行或在详细信息之间切换时触发。
示例:
//Assign a CardView to the relationship
CardView cardView1 = new CardView(gridControl1);
gridControl1.LevelTree.Nodes.Add("CategoriesProducts", cardView1);
//Specify text to be displayed within detail tabs.
cardView1.ViewCaption = "Category Products";
//Hide the CategoryID column for the master View
gridView1.Columns["CategoryID"].VisibleIndex = -1;
//Present data in the Picture column as Images
RepositoryItemPictureEdit riPictureEdit = gridControl1.RepositoryItems.Add("PictureEdit") as RepositoryItemPictureEdit;
gridView1.Columns["Picture"].ColumnEdit = riPictureEdit;
//Stretch images within cells.