在主/从模式中,展开主控行提供了对细节视图的访问。 主控行可以有多个细节视图,每个细节视图都呈现相应主/从关系中的数据。 在主表视图和细节视图中遍历行时必须要重视这一点。
在主控行和细节行中导航
可以通过调用 GridView.ExpandMasterRow、GridView.SetMasterRowExpanded 或 GridView.SetMasterRowExpandedEx 方法打开特定细节视图。 请参阅 展开和折叠主控行 章节获得关于使用这些方法的细节。
一旦细节视图被打开,就可以通过 GridView.GetDetailView 方法访问它。 此方法返回特定主控件行的对应于主/从关系的细节 克隆视图 。 注意,如果不能取得细节视图,则此方法可能返回 null (在 Visual Basic 中为 Nothing)。 例如,如果相应的主/从关系不包含数据,并且 GridOptionsDetail.AllowExpandEmptyDetails 属性值设置为 false,则不创建此关系对应的细节视图。
要获取关于在单个视图内导航的信息,可以参阅 遍历行 文档。 可以允许最终用户通过内嵌的和外部的 导航器控件 导航视图。
有时,可能需要确定当前细节视图的主表视图。 要达到此目的,可以使用 BaseView.ParentView 属性。 BaseView.SourceRowHandle 属性返回指定细节视图的主控行的句柄。 要获得关于行句柄的信息,请参阅 行与卡片识别 主题。
下列代码列出了 NavigateDetails 程序,展示了如何导航主/从行。 此方法遍历指定视图的行。 如果该行有子细节视图,则打开并通过 NavigateDetails 递归调用遍历它们。 行的细节视图的数目通过 GridView.GetRelationCount 方法获取。
细节视图可以是 GridView 或 CardView 的子类。
代码被封闭在 BaseView.BeginUpdate 和 BaseView.EndUpdate 方法调用之中,来防止过度重绘。C# | 复制代码 |
---|---|
using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Base; public void NavigateDetails(ColumnView inspectedView) { if(inspectedView == null) return; // Prevent excessive visual updates. inspectedView.BeginUpdate(); try { GridView gridView = inspectedView as GridView; // Get the number of data rows in the View. int dataRowCount; if(gridView == null) dataRowCount = inspectedView.RowCount; else dataRowCount = gridView.DataRowCount; // Traverse View's rows. for(int rowHandle = 0; rowHandle < dataRowCount; rowHandle++) { // Place your code here to process the current row. // ... if(gridView != null) { // Get the number of master-detail relationships for the current row. int relationCount = gridView.GetRelationCount(rowHandle); // Iterate through master-detail relationships. for(int relationIndex = 0; relationIndex < relationCount; relationIndex++) { // Store expansion status of the corresponding detail View. bool wasExpanded = gridView.GetMasterRowExpandedEx(rowHandle, relationIndex); // Expand the detail View. if(!wasExpanded) gridView.SetMasterRowExpandedEx(rowHandle, relationIndex, true); // Navigate the detail View. NavigateDetails((ColumnView)gridView.GetDetailView(rowHandle, relationIndex)); // Restore the row's expansion status. gridView.SetMasterRowExpandedEx(rowHandle, relationIndex, wasExpanded); } } } } finally { // Enable visual updates. inspectedView.EndUpdate(); } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraGrid.Views.Grid Imports DevExpress.XtraGrid.Views.Base Public Sub NavigateDetails(ByVal inspectedView As ColumnView) If inspectedView Is Nothing Then Return ' Prevent excessive visual updates. inspectedView.BeginUpdate() Try Dim gridView As GridView = Nothing If TypeOf inspectedView Is GridView Then gridView = inspectedView ' Get the number of data rows in the View. Dim dataRowCount As Integer If gridView Is Nothing Then dataRowCount = inspectedView.RowCount Else dataRowCount = gridView.DataRowCount End If ' Traverse View's rows. Dim rowHandle As Integer For rowHandle = 0 To dataRowCount - 1 ' Place your code here to process the current row. ' ... Try inspectedView.SetRowCellValue(rowHandle, inspectedView.Columns(0), rowHandle) Catch ex As Exception End Try If Not gridView Is Nothing Then ' Get the number of master-detail relationships for the current row. Dim relationCount As Integer = gridView.GetRelationCount(rowHandle) ' Iterate through master-detail relationships. Dim relationIndex As Integer For relationIndex = 0 To relationCount - 1 ' Store expansion status of the corresponding detail View. Dim wasExpanded As Boolean = _ gridView.GetMasterRowExpandedEx(rowHandle, relationIndex) ' Expand the detail View. If Not wasExpanded Then gridView.SetMasterRowExpandedEx( _ rowHandle, relationIndex, True) ' Navigate the detail View. NavigateDetails(gridView.GetDetailView(rowHandle, relationIndex)) ' Restore the row's expansion status. gridView.SetMasterRowExpandedEx(rowHandle, relationIndex, wasExpanded) Next End If Next Finally ' Enable visual updates. inspectedView.EndUpdate() End Try End Sub |