Skip to main content
All docs
V25.1
  • How to: Hide Expand Buttons for Master Rows with Empty Details

    • 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:

    Hide Expand Buttons for Master Rows with Empty Details - WinForms Grid | DevExpress

    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;
    }
    

    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.

    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;
        }
    }
    
    See Also