Back to Devexpress

How to: Hide Expand Buttons for Master Rows with Empty Details

windowsforms-403851-controls-and-libraries-data-grid-examples-master-detail-how-to-hide-expand-buttons-for-master-rows-with-empty-details.md

latest4.5 KB
Original Source

How to: Hide Expand Buttons for Master Rows with Empty Details

  • Apr 04, 2022
  • 2 minutes to read

Handle the CustomDrawCell event to hide [+] buttons for master rows. Use the IsMasterRowEmpty(Int32) method to check whether a master row has detail rows. If there are no detail rows, set the CellButtonRect property to Rectangle.Empty.

The screenshot below shows the result:

csharp
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;

private void gridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e) {
    GridView view = sender as GridView;
    if(e.Column.VisibleIndex == 0 && view.IsMasterRowEmpty(e.RowHandle))
        (e.Cell as GridCellInfo).CellButtonRect = Rectangle.Empty;
}
vb
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Imports DevExpress.XtraGrid.Views.Base

Private Sub gridView1_CustomDrawCell(ByVal sender As Object, ByVal e As RowCellCustomDrawEventArgs)
    Dim view As GridView = TryCast(sender, GridView)
    If e.Column.VisibleIndex = 0 AndAlso view.IsMasterRowEmpty(e.RowHandle) Then
        TryCast(e.Cell, GridCellInfo).CellButtonRect = Rectangle.Empty
    End If
End Sub

Master rows can have multiple details (relations). The following example extends the previous one to hide the [+] button for a master row if all its details are empty.

csharp
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;

private void gridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e) {
    GridView view = sender as GridView;
    if(e.Column.VisibleIndex == 0 && 
        view.OptionsDetail.SmartDetailExpandButtonMode != DetailExpandButtonMode.AlwaysEnabled) {
        bool isMasterRowEmpty;
        if(view.OptionsDetail.SmartDetailExpandButtonMode == DetailExpandButtonMode.CheckAllDetails) {
            isMasterRowEmpty = true;
            for(int i = 0; i < view.GetRelationCount(e.RowHandle); i++) {
                if(!view.IsMasterRowEmptyEx(e.RowHandle, i)) {
                    isMasterRowEmpty = false;
                    break;
                }
            }
        } else
            isMasterRowEmpty = view.IsMasterRowEmpty(e.RowHandle);

        // Hides the expand detail button
        if(isMasterRowEmpty)
            (e.Cell as GridCellInfo).CellButtonRect = Rectangle.Empty;
    }
}
vb
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo
Imports DevExpress.XtraGrid.Views.Base

Private Sub gridView1_CustomDrawCell(ByVal sender As Object, ByVal e As RowCellCustomDrawEventArgs)
    Dim view As GridView = TryCast(sender, GridView)
    If e.Column.VisibleIndex = 0 AndAlso view.OptionsDetail.SmartDetailExpandButtonMode <> DetailExpandButtonMode.AlwaysEnabled Then
        Dim isMasterRowEmpty As Boolean
        If view.OptionsDetail.SmartDetailExpandButtonMode = DetailExpandButtonMode.CheckAllDetails Then
            isMasterRowEmpty = True
            For i As Integer = 0 To view.GetRelationCount(e.RowHandle) - 1
                If Not view.IsMasterRowEmptyEx(e.RowHandle, i) Then
                    isMasterRowEmpty = False
                    Exit For
                End If
            Next i
        Else
            isMasterRowEmpty = view.IsMasterRowEmpty(e.RowHandle)
        End If

        ' Hides the expand detail button
        If isMasterRowEmpty Then
            TryCast(e.Cell, GridCellInfo).CellButtonRect = Rectangle.Empty
        End If
    End If
End Sub

See Also

IsMasterRowEmpty(Int32)

SmartDetailExpandButtonMode

Master-Detail Relationships