DataGrid 嵌套绑定显示复合报表

原创 2004年07月12日 17:51:00

以 SQL2000 中的 Northwind 数据库为例演示实现代码

// WebForm1.aspx 代码

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="DemoPortal.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <STYLE type="text/css">
 A:link { COLOR: #000000; TEXT-DECORATION: none }
 A:active { COLOR: #000000; TEXT-DECORATION: none }
 A:visited { COLOR: #000000; TEXT-DECORATION: none }
 A:hover { COLOR: #4455aa; TEXT-DECORATION: underline }
 BODY { SCROLLBAR-FACE-COLOR: #dee3e7; FONT-SIZE: 11px; SCROLLBAR-HIGHLIGHT-COLOR: #ffffff; SCROLLBAR-SHADOW-COLOR: #dee3e7; COLOR: #000000; SCROLLBAR-3DLIGHT-COLOR: #d1d7dc; SCROLLBAR-ARROW-COLOR: #006699; SCROLLBAR-TRACK-COLOR: #efefef; FONT-FAMILY: Verdana,宋体; SCROLLBAR-DARKSHADOW-COLOR: #98aab1 }
 FONT { LINE-HEIGHT: normal }
 TD { FONT-SIZE: 11px; LINE-HEIGHT: 15px; FONT-FAMILY: Verdana,宋体 }
 TH { FONT-WEIGHT: bold; FONT-SIZE: 12px; BACKGROUND-IMAGE: url(Skins/Default/css/default/bg1.gif); COLOR: white; BACKGROUND-COLOR: #4455aa }
 TD.TableTitle2 { BACKGROUND-COLOR: #e4e8ef }
 TD.TableBody1 { LINE-HEIGHT: normal; BACKGROUND-COLOR: #ffffff }
 TD.TableBody2 { LINE-HEIGHT: normal; BACKGROUND-COLOR: #e4e8ef }
 TD.TopDarkNav { BACKGROUND-IMAGE: url(Skins/Default/css/default/topbg.gif) }
 TD.TopLighNav { BACKGROUND-IMAGE: url(Skins/Default/css/default/bottombg.gif) }
 TD.TopLighNav1 { BACKGROUND-IMAGE: url(Skins/Default/css/default/tabs_m_tile.gif) }
 TD.TopLighNav2 { BACKGROUND-COLOR: #ffffff }
 .tableBorder1 { BORDER-RIGHT: 1px; BORDER-TOP: 1px; BORDER-LEFT: 1px; WIDTH: 98%; BORDER-BOTTOM: 1px; BACKGROUND-COLOR: #6595d6 }
 .tableBorder2 { BORDER-RIGHT: #dedede 1px solid; BORDER-TOP: #dedede 1px solid; BORDER-LEFT: #dedede 1px solid; WIDTH: 98%; BORDER-BOTTOM: #dedede 1px solid; BACKGROUND-COLOR: #efefef }
 #TableTitleLink A:link { COLOR: #ffffff; TEXT-DECORATION: none }
 #TableTitleLink A:visited { COLOR: #ffffff; TEXT-DECORATION: none }
 #TableTitleLink A:active { COLOR: #ffffff; TEXT-DECORATION: none }
 #TableTitleLink A:hover { COLOR: #ffffff; TEXT-DECORATION: underline }
 INPUT { FONT-SIZE: 12px; COLOR: #000000; LINE-HEIGHT: 15px; FONT-FAMILY: Tahoma,Verdana,"宋体" }
 SELECT { FONT-SIZE: 12px; COLOR: #000000; LINE-HEIGHT: 15px; FONT-FAMILY: Tahoma,Verdana,"宋体" }
 TEXTAREA { FONT-SIZE: 12px; COLOR: #000000; LINE-HEIGHT: 15px; FONT-FAMILY: Tahoma,Verdana,"宋体" }
 OPTION { FONT-SIZE: 12px; COLOR: #000000; LINE-HEIGHT: 15px; FONT-FAMILY: Tahoma,Verdana,"宋体" }
 .normalTextSmall { FONT-SIZE: 11px; COLOR: #000000; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif }
 .menuskin { BORDER-RIGHT: #666666 1px solid; BORDER-TOP: #666666 1px solid; BACKGROUND-IMAGE: url(Skins/Default/dvmenubg3.gif); VISIBILITY: hidden; FONT: 12px Verdana; BORDER-LEFT: #666666 1px solid; BORDER-BOTTOM: #666666 1px solid; BACKGROUND-REPEAT: repeat-y; POSITION: absolute; BACKGROUND-COLOR: #efefef }
 .menuskin A { PADDING-RIGHT: 10px; PADDING-LEFT: 25px; BEHAVIOR: url(inc/noline.htc); COLOR: black; TEXT-DECORATION: none }
 #mouseoverstyle { BORDER-RIGHT: #597db5 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #597db5 1px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 2px; BORDER-LEFT: #597db5 1px solid; PADDING-TOP: 0px; BORDER-BOTTOM: #597db5 1px solid; BACKGROUND-COLOR: #c9d5e7 }
 #mouseoverstyle A { COLOR: black }
 .menuitems { PADDING-RIGHT: 1px; PADDING-LEFT: 1px; PADDING-BOTTOM: 1px; MARGIN: 2px; WORD-BREAK: keep-all; PADDING-TOP: 1px }
 A.navlink:link { COLOR: #000000; TEXT-DECORATION: none }
 A.navlink:visited { COLOR: #000000; TEXT-DECORATION: none }
 A.navlink:hover { COLOR: #003399; TEXT-DECORATION: none }
 .BrightClass { BACKGROUND-COLOR: #d7d7d7 }
 DIV.quote { BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 5px; BACKGROUND: #f3f3f3; PADDING-BOTTOM: 5px; MARGIN: 5px 20px; BORDER-LEFT: #cccccc 1px solid; LINE-HEIGHT: normal; PADDING-TOP: 5px; BORDER-BOTTOM: #cccccc 1px solid }
 DIV.HtmlCode { BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 5px; FONT-SIZE: 12px; BACKGROUND: #fdfddf; PADDING-BOTTOM: 5px; MARGIN: 5px 20px; OVERFLOW: auto; BORDER-LEFT: #cccccc 1px solid; LINE-HEIGHT: normal; PADDING-TOP: 5px; BORDER-BOTTOM: #cccccc 1px solid; FONT-FAMILY: ncursive }
  </STYLE>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body>
  <form id="Form1" method="post" runat="server">
   <asp:Button id="Bn_Demo" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute; TOP: 16px" runat="server"
    Text="Demo"></asp:Button>
   <table>
    <tr>
     <td>
      <asp:DataGrid id="DG_Order" style="Z-INDEX: 101; LEFT: 32px; POSITION: absolute; TOP: 56px" runat="server"
       ShowHeader="False" AutoGenerateColumns="False">
       <Columns>
        <asp:BoundColumn DataField="OrderID" Visible="False"></asp:BoundColumn>
        <asp:BoundColumn DataField="OrderDate" Visible="False"></asp:BoundColumn>
        <asp:BoundColumn DataField="RequiredDate" Visible="False"></asp:BoundColumn>
        <asp:BoundColumn DataField="ShippedDate" Visible="False"></asp:BoundColumn>
        <asp:BoundColumn DataField="Freight" Visible="False"></asp:BoundColumn>
        <asp:TemplateColumn>
         <ItemTemplate>
          <table>
           <tr>
            <td>
             <asp:Label ID="Lb_OrderDate" Runat="server">2004-07-15</asp:Label>
             <asp:Label ID="Lb_RequiredDate" Runat="server">2004-07-15</asp:Label>
             <asp:Label ID="Lb_ShippedDate" Runat="server">2004-07-15</asp:Label>
             <asp:Label ID="Lb_Freight" Runat="server">2004-07-15</asp:Label>
            </td>
           </tr>
           <tr>
            <td>
             <table>
              <tr>
               <td>
                <asp:DataGrid ID="DG_Category" AutoGenerateColumns="False" Runat="server" ShowHeader="False">
                 <Columns>
                  <asp:BoundColumn DataField="CategoryID" Visible="False"></asp:BoundColumn>
                  <asp:BoundColumn DataField="OrderID" Visible="False"></asp:BoundColumn>
                  <asp:BoundColumn DataField="CategoryName" ItemStyle-Font-Bold="True" ItemStyle-HorizontalAlign="Center"
                   ItemStyle-VerticalAlign="Middle">
                   <ItemStyle Width="120px"></ItemStyle>
                  </asp:BoundColumn>
                  <asp:TemplateColumn>
                   <ItemTemplate>
                    <table>
                     <tr>
                      <td>
                       <asp:DataGrid ID="DG_OrderDetail" AutoGenerateColumns="False" Runat="server" ShowHeader="False">
                        <Columns>
                         <asp:BoundColumn DataField="productname">
                          <ItemStyle Width="300px"></ItemStyle>
                         </asp:BoundColumn>
                         <asp:BoundColumn DataField="unitprice">
                          <ItemStyle Width="90px"></ItemStyle>
                         </asp:BoundColumn>
                         <asp:BoundColumn DataField="quantity">
                          <ItemStyle Width="50px"></ItemStyle>
                         </asp:BoundColumn>
                         <asp:BoundColumn DataField="discount">
                          <ItemStyle Width="40px"></ItemStyle>
                         </asp:BoundColumn>
                        </Columns>
                       </asp:DataGrid>
                      </td>
                     </tr>
                    </table>
                   </ItemTemplate>
                  </asp:TemplateColumn>
                 </Columns>
                </asp:DataGrid>
               </td>
              </tr>
             </table>
            </td>
           </tr>
          </table>
         </ItemTemplate>
        </asp:TemplateColumn>
       </Columns>
      </asp:DataGrid>
     </td>
    </tr>
   </table>
  </form>
 </body>
</HTML>

 

// WebForm1.aspx.cs 代码

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DemoPortal
{
 ///
 /// WebForm1 的摘要说明。
 ///
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button Bn_Demo;
  protected System.Web.UI.WebControls.DataGrid DG_Order;

  SqlConnection Cn;     // 全局数据连接
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   Cn = new SqlConnection("server=(local);DataBase=northwind;uid=sa;pwd=4118615");
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  ///
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///
  private void InitializeComponent()
  {   
   this.Bn_Demo.Click += new System.EventHandler(this.Bn_Demo_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Bn_Demo_Click(object sender, System.EventArgs e)
  {
   // 取定单数据集
   SqlDataAdapter SqlDa = new SqlDataAdapter("select * from orders order by orderdate",Cn);
   DataSet ds = new DataSet();
   SqlDa.Fill(ds);

   // 设置绑定函数
   this.DG_Order.ItemDataBound +=new DataGridItemEventHandler(DG_Order_ItemDataBound);

   // 绑定显示控件
   this.DG_Order.DataSource = ds;
   this.DG_Order.DataBind();
  }

  private void DG_Order_ItemDataBound(object sender, DataGridItemEventArgs e)
  {
   // 判断是表头还是表列
   if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
    Label Lb_OrderDate = (Label)(e.Item.Cells[5].FindControl("Lb_OrderDate"));
    Label Lb_RequiredDate = (Label)(e.Item.Cells[5].FindControl("Lb_RequiredDate"));
    Label Lb_ShippedDate = (Label)(e.Item.Cells[5].FindControl("Lb_ShippedDate"));
    Label Lb_Freight = (Label)(e.Item.Cells[5].FindControl("Lb_Freight"));

    Lb_OrderDate.Text = "下单:" + e.Item.Cells[1].Text;
    Lb_RequiredDate.Text = " 交期:" + e.Item.Cells[2].Text;
    Lb_ShippedDate.Text = " 船期:" + e.Item.Cells[3].Text;
    Lb_Freight.Text =  " 空运费:" + e.Item.Cells[4].Text;

    // 绑定产品类别
    string strOrderID = e.Item.Cells[0].Text;
    string strSQL = "select distinct a.categoryid, a.categoryname, c.orderid from categories a, products b, /"order details/" c where a.categoryid=b.categoryid and b.productid=c.productid and c.orderid=" + strOrderID;

    SqlDataAdapter SqlDa = new SqlDataAdapter(strSQL, Cn);
    DataSet ds = new DataSet();
    SqlDa.Fill(ds);

    // 取 DataGrid 绑定
    if (ds.Tables[0].Rows.Count>0)
    {
     DataGrid DG_Category = (DataGrid)(e.Item.Cells[5].FindControl("DG_Category"));
     DG_Category.ItemDataBound +=new DataGridItemEventHandler(DG_Category_ItemDataBound);
     DG_Category.DataSource = ds;
     DG_Category.DataBind();
    }
   }
  }

  private void DG_Category_ItemDataBound(object sender, DataGridItemEventArgs e)
  {
   // 判断是表头还是表列
   if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
    // 查询 产品定单明细 类别
    string strOrderID = e.Item.Cells[1].Text;
    string strCategoryID = e.Item.Cells[0].Text;
    string strSQL = "select b.productname, c.unitprice, c.quantity, c.discount from categories a, products b, /"order details/" c where a.categoryid=b.categoryid and b.productid=c.productid and c.orderid=" + strOrderID+" and b.categoryid="+strCategoryID;

    SqlDataAdapter SqlDa = new SqlDataAdapter(strSQL, Cn);
    DataSet ds = new DataSet();
    SqlDa.Fill(ds);

    // 取 DataGrid 绑定
    if (ds.Tables[0].Rows.Count>0)
    {
     DataGrid DG_OrderDetail = (DataGrid)(e.Item.Cells[3].FindControl("DG_OrderDetail"));
     DG_OrderDetail.DataSource = ds;
     DG_OrderDetail.DataBind();
    }

    // 没有标题栏,ToolTip 提示
    e.Item.ToolTip = "货品:名称,价格,数量,折扣";
   }
  }
 }
}

jquery easyui datagrid列绑定嵌套对象

1. json的数据格式:       var map = {"uname":'',"upwd":'',"role":{“rolename”:'',"roleLevel":''}} 把这个...
  • a9529lty
  • a9529lty
  • 2013年07月25日 15:32
  • 4602

EasyUI DataGrid绑定嵌套的json数据

现有json数据如下: {"total":2,"rows": [{"BarrackGUID":"dc1be891-4489-4eb8-bb1d-ba1debd59b07","BarrackNam...
  • zdhlwt2008
  • zdhlwt2008
  • 2016年10月25日 08:53
  • 1510

如何绑定IList集合中对象的复合属性到DataGrid中。

有如下代码: /**//* * Created by:  * Created: 2006年12月23日 */using System;namespace TestObjectToDataGrid......
  • ScottHoward
  • ScottHoward
  • 2006年12月23日 20:38
  • 1237

jquery easyui datagrid 实现嵌套表格

subGrid,也就是datagrid中再套一层datagrid,比较适合用于显示一些带有层次的数据,比如要显示一批订单,订单包含具体的货物。这样一个需求,就可以用到subGrid。 我们可以先看看...
  • zhu_nana
  • zhu_nana
  • 2015年11月23日 11:18
  • 9727

easyUI下datagrid嵌套显示

主要是运用 easyui的方法进行行内嵌套显示,让一行可以显示更多的东西,让一行显示JSON的字符串、从表之类的东西。下边先看效果图: 接下来说实现过程: (1)在首次加载中添加代码view: d...
  • shenyou_fu
  • shenyou_fu
  • 2017年07月27日 17:09
  • 475

让jquery easyui datagrid列支持绑定嵌套对象

嵌套对象是指返回的json数据,是对象的某个属性自带有属性。而我们恰恰又需要这个属性,默认情况下easyui的datagrid是不支持绑定嵌套对象的。比如:datagrid的field属性只能为fie...
  • xiaozaq
  • xiaozaq
  • 2016年11月28日 16:27
  • 1144

easyUI折叠表格层次显示detailview详解及实例(本实例3层嵌套)

本文目的: 使用easyUi的特殊常用实例,表格层次显示,一层套一层,显示详细数据,嵌套3层应该可以满足所有人的需求了吧。如果你想嵌套4层,有了嵌套3层的案例,4层,5层不是问题吧!!! 下...
  • meng564764406
  • meng564764406
  • 2016年12月28日 15:56
  • 3554

如何在DataGrid中访问复合对象属性

//定义用于时间显示的回调函数private function dateLabel(item:Object,column:DataGridColumn):String{ return dateForm...
  • xinxin19881112
  • xinxin19881112
  • 2011年01月18日 01:08
  • 1104

jquery-easyui拓展之datagrid复合表头列锁定/解锁和列隐藏/显示

jquery-easyui本身没有提供列锁定/解锁的接口,并且其原有的列隐藏/显示在符合表头的情况下会出现错位,我在项目中遇到了这两个问题,在参考了网上许多解决方案后,编写了一个拓展插件,基本上解决了...
  • tianxiawudi0720
  • tianxiawudi0720
  • 2015年08月10日 17:08
  • 1883

EasyUI datagrid json嵌套的object数据在datagrid中的显示方法

案例使用员工emp和部门dept来演示。实体对象 emp和deptpublic class Dept implements java.io.Serializable { private Int...
  • qq_34873338
  • qq_34873338
  • 2017年05月04日 21:48
  • 969
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:DataGrid 嵌套绑定显示复合报表
举报原因:
原因补充:

(最多只允许输入30个字)