zoukankan      html  css  js  c++  java
  • ASPxGridView中DetailRow的使用

    ASPxGridView是一个方便的数据显示控件,可是自动的绑定我们所需要的数据,但是有时,当数据属性过多时,我们并不一定要把所有的信息提供给所有的人,当有人需要这些数据时可以自动的进行查看,这时就可以把一些主要的信息提供给所有人,对于一些细节信息,可以按照个人的需求就行显示,这就要用到ASPxGridView的DetailRow了,本文将以铁路列车信息数据库为依据,首先显所有的列车车次,点击后查看列车详情,效果图如下:

    ASPxGridView中DetailRow的使用

    ASPxGridView中DetailRow的使用

     本文实现的效果和前面介绍的ASPxPop控件的效果类似,但是这个控件也可以用在不方便使用那和空间的时候,读者可以对比博文http://blog.sina.com.cn/s/blog_62cd5a980100v6xx.html,进行选择,上代码:

    前端代码:

     

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

     

    <%@ Register Assembly="DevExpress.Web.ASPxGridView.v9.2, Version=9.2.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"

        Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv" %>

    <%@ Register Assembly="DevExpress.Web.ASPxEditors.v9.2, Version=9.2.2.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"

        Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>

    <!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>具有详细信息的AspGridView测试</title>

    </head>

    <body>

        <form id="form1" runat="server">

        <div align="center">

            <dxwgv:ASPxGridView ID="ASPxGridView1" runat="server" CssFilePath="~/App_Themes/Office2003Blue/{0}/styles.css"

                CssPostfix="Office2003_Blue" Caption="全国火车车次信息表" KeyFieldName="ID" ClientInstanceName="grid">

                <Styles CssFilePath="~/App_Themes/Office2003Blue/{0}/styles.css" CssPostfix="Office2003_Blue">

                    <Header ImageSpacing="5px" SortingImageSpacing="5px">

                    </Header>

                    <LoadingPanel ImageSpacing="10px">

                    </LoadingPanel>

                </Styles>

                <Images ImageFolder="~/App_Themes/Office2003Blue/{0}/">

                    <CollapsedButton Height="12px" Url="~/App_Themes/Office2003Blue/GridView/gvCollapsedButton.png"

                        Width="11px" />

                    <ExpandedButton Height="12px" Url="~/App_Themes/Office2003Blue/GridView/gvExpandedButton.png"

                        Width="11px" />

                    <DetailCollapsedButton Height="12px" Url="~/App_Themes/Office2003Blue/GridView/gvCollapsedButton.png"

                        Width="11px" />

                    <DetailExpandedButton Height="12px" Url="~/App_Themes/Office2003Blue/GridView/gvExpandedButton.png"

                        Width="11px" />

                    <FilterRowButton Height="13px" Width="13px" />

                </Images>

                <StylesEditors>

                    <ProgressBar Height="25px">

                    </ProgressBar>

                </StylesEditors>

                <SettingsPager PageSize="15">

                </SettingsPager>

                <SettingsDetail ShowDetailRow="true" AllowOnlyOneMasterRowExpanded="True" ExportMode="All" />

                <ClientSideEvents RowClick="function(s, e) {

                _aspxClearSelection();

                grid._selectAllRowsOnPage(false);

                grid.SelectRow(e.visibleIndex, true);

    }" />

                <Columns>

                    <dxwgv:GridViewDataColumn Caption="序号">

                        <DataItemTemplate>

                            <%#Container.ItemIndex+1 %>

                        </DataItemTemplate>

                    </dxwgv:GridViewDataColumn>

                    <dxwgv:GridViewDataColumn Caption="车次" FieldName="ID" Width="200PX">

                    </dxwgv:GridViewDataColumn>

                    <dxwgv:GridViewDataColumn Caption="列车类型" FieldName="type" Width="200PX">

                    </dxwgv:GridViewDataColumn>

                </Columns>

                <Templates>

                    <DetailRow>

                        <dxwgv:ASPxGridView ID="ASPxGridView2" runat="server" CssFilePath="~/App_Themes/Office2003Blue/{0}/styles.css"

                            CssPostfix="Office2003_Blue" OnDataBinding="getnewdata">

                            <Styles CssFilePath="~/App_Themes/Office2003Blue/{0}/styles.css" CssPostfix="Office2003_Blue">

                                <Header ImageSpacing="5px" SortingImageSpacing="5px">

                                </Header>

                                <LoadingPanel ImageSpacing="10px">

                                </LoadingPanel>

                            </Styles>

                            <SettingsPager PageSize="10">

                            </SettingsPager>

                            <SettingsDetail IsDetailGrid="true" />

                            <Images ImageFolder="~/App_Themes/Office2003Blue/{0}/">

                                <CollapsedButton Height="12px" Url="~/App_Themes/Office2003Blue/GridView/gvCollapsedButton.png"

                                    Width="11px" />

                                <ExpandedButton Height="12px" Url="~/App_Themes/Office2003Blue/GridView/gvExpandedButton.png"

                                    Width="11px" />

                                <DetailCollapsedButton Height="12px" Url="~/App_Themes/Office2003Blue/GridView/gvCollapsedButton.png"

                                    Width="11px" />

                                <DetailExpandedButton Height="12px" Url="~/App_Themes/Office2003Blue/GridView/gvExpandedButton.png"

                                    Width="11px" />

                                <FilterRowButton Height="13px" Width="13px" />

                            </Images>

                            <StylesEditors>

                                <ProgressBar Height="25px">

                                </ProgressBar>

                            </StylesEditors>

                            <Columns>

                                <dxwgv:GridViewDataColumn Caption="序号">

                                    <DataItemTemplate>

                                        <%#Container.ItemIndex+1 %>

                                    </DataItemTemplate>

                                </dxwgv:GridViewDataColumn>

                                <dxwgv:GridViewDataColumn Caption="车次" FieldName="ID">

                                </dxwgv:GridViewDataColumn>

                                <dxwgv:GridViewDataColumn Caption="到站" FieldName="Station">

                                </dxwgv:GridViewDataColumn>

                                <dxwgv:GridViewDataColumn Caption="到站时间" FieldName="A_Time">

                                </dxwgv:GridViewDataColumn>

                                <dxwgv:GridViewDataColumn Caption="发车时间" FieldName="D_Time">

                                </dxwgv:GridViewDataColumn>

                                <dxwgv:GridViewDataColumn Caption="天数" FieldName="Day">

                                </dxwgv:GridViewDataColumn>

                                <dxwgv:GridViewDataColumn Caption="里程" FieldName="Distance">

                                </dxwgv:GridViewDataColumn>

                                <dxwgv:GridViewDataColumn Caption="一等座" FieldName="P1">

                                </dxwgv:GridViewDataColumn>

                                <dxwgv:GridViewDataColumn Caption="二等座" FieldName="P2">

                                </dxwgv:GridViewDataColumn>

                                <dxwgv:GridViewDataColumn Caption="三等座" FieldName="P3">

                                </dxwgv:GridViewDataColumn>

                                <dxwgv:GridViewDataColumn Caption="四等座" FieldName="P4">

                                </dxwgv:GridViewDataColumn>

                            </Columns>

                        </dxwgv:ASPxGridView>

                    </DetailRow>

                </Templates>

            </dxwgv:ASPxGridView>

        </div>

        </form>

    </body>

    </html>

    后端代码:
    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.OleDb;
    using DevExpress.Web.ASPxGridView;
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AspxGridViewDataBind();
            }
            else
            {
                if (ViewState["ASPxGridView1"] !=null)
                {
                    DataSet ds = ViewState["ASPxGridView1"] as DataSet;
                    this.ASPxGridView1.DataSource = ds;
                    this.ASPxGridView1.DataBind();
                }
            }
     
        }
        public OleDbConnection getConnection() 
        {
            OleDbConnection conn = null;
            string connectionstr = "provider=Microsoft.JET.OLEDB.4.0;";
            connectionstr += @"data source=d:database rain.mdb";
            try
            {
                conn = new OleDbConnection(connectionstr);
                conn.Open();
                //ScriptManager.RegisterStartupScript(this,this.GetType(),"","alert('建立连接成功!');",true);
            }
            catch (Exception e)
            {
                
                Response.Write("建立数据库连接失败!"+e.Message);
            }
            return conn;
        }
        public void closeConnection(OleDbConnection conn) 
        {
            try
            {
                conn.Close();
            }
            catch (Exception e)
            {
                
                Response.Write("关闭数据连接失败!"+e.Message);
            }
        }
        public DataSet getDataSet(OleDbConnection conn) 
        {
            DataSet ds = new DataSet();
            string sql = "select distinct ID,type from train ";
            //string sql="select * from train";
            try
            {
                OleDbDataAdapter oda = new OleDbDataAdapter(sql,conn);
                oda.Fill(ds);
            }
            catch (Exception e)
            {
     
                Response.Write("获取结果集失败!"+e.Message) ;
            }
            return ds;
        }
        public void display() 
        {
            OleDbConnection conn = getConnection();
            DataSet ds = getDataSet(conn);
            for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
            {
                Response.Write(ds.Tables[0].Columns[i].ColumnName.ToString()+"<br/>");
            }
            closeConnection(conn);
        }
        public void AspxGridViewDataBind()
        {
            OleDbConnection conn = getConnection();
            DataSet ds = getDataSet(conn);
            this.ASPxGridView1.DataSource = ds;
            this.ASPxGridView1.DataBind();
            ViewState["ASPxGridView1"] = ds;
            closeConnection(conn);
        }
        public void getnewdata(object sender,EventArgs e) 
        {
            ASPxGridView grid = (ASPxGridView)sender;
            string id = grid.GetMasterRowFieldValues("ID").ToString();
            //this.Response.Write(id);
            DataSet ds = getDetailData(id);
            grid.DataSource = ds;
            //ViewState["grid"] = ds;
        }
        public DataSet getDetailData(string id) 
        {
            DataSet ds = new DataSet();
            string sql = "select * from train where id='"+id+"'";
            OleDbConnection conn = getConnection();
            OleDbDataAdapter oda = new OleDbDataAdapter(sql,conn);
            oda.Fill(ds);
            closeConnection(conn);
            return ds;
        }
    }
     
    源地址:http://blog.sina.com.cn/s/blog_62cd5a980100vmge.html
  • 相关阅读:
    中国广电工信战争
    围观一个People Search
    Popular榜单能做到小众化吗?
    校园招聘:内地大学生的视野和实践有问题吗?
    锐推榜的平衡策略
    PyQt 自定义信号带参数 emit
    Python pyinstaller
    Python 匿名函数 lambda
    PyQT5 绑定函数的传参(connect 带参数)
    Excel 一列文本变为数字
  • 原文地址:https://www.cnblogs.com/haizine/p/5806957.html
Copyright © 2011-2022 走看看