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();
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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>
        <br />
        <br />
    </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>

 

ตัวอย่างมี Gridview 1 2 3

    protected void ASPxGridView2_DataBinding(object sender, EventArgs e)
    {
        DoSelect();
    }
    private void DoSelect()
    {
        DataView selectResult = new DataView();
        string selectCommand = "select * from [SubBudgetPlan] WHERE [SubCat] LIKE '2.3 ค่าวัสดุ'";
        Database db = DatabaseFactory.CreateDatabase();
        DbCommand dbCommand = db.GetSqlStringCommand(selectCommand);
        DataSet ds = db.ExecuteDataSet(dbCommand);
        selectResult = ds.Tables[0].DefaultView;

        ArrayList result = new ArrayList();
        foreach (DataRow row in selectResult.Table.Rows)
            result.Add(row["SubID"]); // ต้องตรงกับ ASPxGridView3_BeforePerformDataSelect ในส่วน SubID
        Session["SelectResult"] = result;
    }
    protected void ASPxGridView2_DetailRowGetButtonVisibility
    (object sender, ASPxGridViewDetailRowButtonEventArgs e)
    {
        if (!((ArrayList)Session["SelectResult"]).Contains(e.KeyValue))
            e.ButtonState = GridViewDetailRowButtonState.Hidden;
    }
    protected void ASPxGridView3_BeforePerformDataSelect(object sender, EventArgs e)
    {
        Session["ssSubID"] = (sender as ASPxGridView).GetMasterRowKeyValue();

    }

 

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.