Back to Devexpress

How to: Hide Expand Buttons for Master Rows That Have Empty Details

aspnet-5334-components-grid-view-examples-how-to-hide-expand-buttons-for-master-rows-that-have-empty-details.md

latest10.2 KB
Original Source

How to: Hide Expand Buttons for Master Rows That Have Empty Details

  • Oct 01, 2021
  • 3 minutes to read

This example uses the server-side DetailRowGetButtonVisibility event to hide the detail button for "empty" detail rows.

View Example

aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="DevExpress.Web.v13.2, Version=13.2.13.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v13.2, Version=13.2.13.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to hide the detail button if the detail grid is empty</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        

        

    </div>
    <dx:ASPxGridView ID="mainGrid" runat="server" AutoGenerateColumns="False" DataSourceID="masterDataSource"
        KeyFieldName="CategoryID" OnDataBinding="masterGrid_DataBinding" OnDetailRowGetButtonVisibility="masterGrid_DetailRowGetButtonVisibility">
        <Templates>
            <DetailRow>
                <dx:ASPxGridView ID="detailGrid" runat="server" AutoGenerateColumns="False" DataSourceID="dsDetail"
                    KeyFieldName="ProductID" OnBeforePerformDataSelect="detailGrid_BeforePerformDataSelect"
                    Width="100%">
                    <Styles>
                        <DetailRow HorizontalAlign="Justify">
                        </DetailRow>
                    </Styles>
                    <Columns>
                        <dx:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" VisibleIndex="0">
                            <EditFormSettings Visible="False" />
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1">
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="UnitPrice" VisibleIndex="2">
                        </dx:GridViewDataTextColumn>
                    </Columns>
                </dx:ASPxGridView>
            </DetailRow>
        </Templates>
        <Columns>
            <dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" VisibleIndex="0">
                <EditFormSettings Visible="False" />
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="1">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="2">
            </dx:GridViewDataTextColumn>
        </Columns>
        <SettingsDetail ShowDetailRow="True" />
    </dx:ASPxGridView>
    <asp:AccessDataSource ID="dsDetail" runat="server" DataFile="~/App_Data/nwind.mdb"
        SelectCommand="SELECT [ProductID], [ProductName], [CategoryID], [UnitPrice] FROM [Products] WHERE ([CategoryID] = ?)">
        <SelectParameters>
            <asp:SessionParameter DefaultValue="CategoryID" Name="CategoryID" SessionField="CategoryID"
                Type="Int32" />
        </SelectParameters>
    </asp:AccessDataSource>
    <asp:AccessDataSource ID="masterDataSource" runat="server" DataFile="~/App_Data/nwind.mdb"
        SelectCommand="SELECT * FROM [Categories]"></asp:AccessDataSource>
    </form>
</body>
</html>
csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;
using System.Drawing;
using System.Collections;
using System.Data;

public partial class _Default : System.Web.UI.Page {
    protected void masterGrid_DataBinding (object sender, EventArgs e) {
        DoSelect(masterDataSource.DataFile);
    }
    private void DoSelect (string connectionString) {
        DataView selectResult = new DataView();
        string selectCommand = "select distinct [CategoryID] from [Products]";
        using (AccessDataSource ds = new AccessDataSource(connectionString, selectCommand)) {
            selectResult = (DataView)ds.Select(DataSourceSelectArguments.Empty);
        }
        ArrayList result = new ArrayList();
        foreach (DataRow row in selectResult.Table.Rows)
            result.Add(row["CategoryID"]);
        Session["SelectResult"] = result;
    }
    protected void masterGrid_DetailRowGetButtonVisibility
    (object sender, ASPxGridViewDetailRowButtonEventArgs e) {
        if (!((ArrayList)Session["SelectResult"]).Contains(e.KeyValue))
            e.ButtonState = GridViewDetailRowButtonState.Hidden;
    }
    protected void detailGrid_BeforePerformDataSelect (object sender, EventArgs e) {
        Session["CategoryID"] = (sender as ASPxGridView).GetMasterRowKeyValue();
    }
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DevExpress.Web.ASPxGridView
Imports DevExpress.Web.Data
Imports System.Drawing
Imports System.Collections
Imports System.Data

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub masterGrid_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
        DoSelect(masterDataSource.DataFile)
    End Sub
    Private Sub DoSelect(ByVal connectionString As String)
        Dim selectResult As New DataView()
        Dim selectCommand As String = "select distinct [CategoryID] from [Products]"
        Using ds As New AccessDataSource(connectionString, selectCommand)
            selectResult = CType(ds.Select(DataSourceSelectArguments.Empty), DataView)
        End Using
        Dim result As New ArrayList()
        For Each row As DataRow In selectResult.Table.Rows
            result.Add(row("CategoryID"))
        Next row
        Session("SelectResult") = result
    End Sub
    Protected Sub masterGrid_DetailRowGetButtonVisibility(ByVal sender As Object, ByVal e As ASPxGridViewDetailRowButtonEventArgs)
        If Not(CType(Session("SelectResult"), ArrayList)).Contains(e.KeyValue) Then
            e.ButtonState = GridViewDetailRowButtonState.Hidden
        End If
    End Sub
    Protected Sub detailGrid_BeforePerformDataSelect(ByVal sender As Object, ByVal e As EventArgs)
        Session("CategoryID") = (TryCast(sender, ASPxGridView)).GetMasterRowKeyValue()
    End Sub
End Class
aspx
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register Assembly="DevExpress.Web.ASPxGridView.v10.1, Version=10.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v10.1, Version=10.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to hide the detail button if the detail grid is empty</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        

        

    </div>
    <dx:ASPxGridView ID="mainGrid" runat="server" AutoGenerateColumns="False" DataSourceID="masterDataSource"
        KeyFieldName="CategoryID" OnDataBinding="masterGrid_DataBinding" OnDetailRowGetButtonVisibility="masterGrid_DetailRowGetButtonVisibility">
        <Templates>
            <DetailRow>
                <dx:ASPxGridView ID="detailGrid" runat="server" AutoGenerateColumns="False" DataSourceID="dsDetail"
                    KeyFieldName="ProductID" OnBeforePerformDataSelect="detailGrid_BeforePerformDataSelect"
                    Width="100%">
                    <Styles>
                        <DetailRow HorizontalAlign="Justify">
                        </DetailRow>
                    </Styles>
                    <Columns>
                        <dx:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" VisibleIndex="0">
                            <EditFormSettings Visible="False" />
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1">
                        </dx:GridViewDataTextColumn>
                        <dx:GridViewDataTextColumn FieldName="UnitPrice" VisibleIndex="2">
                        </dx:GridViewDataTextColumn>
                    </Columns>
                </dx:ASPxGridView>
            </DetailRow>
        </Templates>
        <Columns>
            <dx:GridViewDataTextColumn FieldName="CategoryID" ReadOnly="True" VisibleIndex="0">
                <EditFormSettings Visible="False" />
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="CategoryName" VisibleIndex="1">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="2">
            </dx:GridViewDataTextColumn>
        </Columns>
        <SettingsDetail ShowDetailRow="True" />
    </dx:ASPxGridView>
    <asp:AccessDataSource ID="dsDetail" runat="server" DataFile="~/App_Data/nwind.mdb"
        SelectCommand="SELECT [ProductID], [ProductName], [CategoryID], [UnitPrice] FROM [Products] WHERE ([CategoryID] = ?)">
        <SelectParameters>
            <asp:SessionParameter DefaultValue="CategoryID" Name="CategoryID" SessionField="CategoryID"
                Type="Int32" />
        </SelectParameters>
    </asp:AccessDataSource>
    <asp:AccessDataSource ID="masterDataSource" runat="server" DataFile="~/App_Data/nwind.mdb"
        SelectCommand="SELECT * FROM [Categories]"></asp:AccessDataSource>
    </form>
</body>
</html>