CSDN博客

img david_zl

DataGrid 嵌套绑定显示复合报表

发表于2004/7/12 17:51:00  3430人阅读

分类: C# 练手

以 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 = "货品:名称,价格,数量,折扣";
   }
  }
 }
}

0 0

相关博文

我的热门文章

img
取 消
img