使用DataGrid控件实现以主细表显示数据

<%@ Page language="c#" Codebehind="DataGridMasterFrom.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.DataGridMasterFrom" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>DataGridMasterFrom</title>
  <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 MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <h2>使用DataGrid控件实现以主细表显示数据</h2>
   <asp:DataGrid id="dgMaster" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 64px" runat="server"
    AutoGenerateColumns="False" ShowHeader="False" Width="368px">
    <Columns>
     <asp:TemplateColumn>
      <ItemTemplate>
       <TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
        <TR>
         <TD bgColor="#3399ff"><FONT color="#ffffff"><B>订单号:
            <%# DataBinder.Eval(Container.DataItem, "OrderID") %>
           </B></FONT>
         </TD>
        </TR>
        <TR>
         <TD align="right">
          <asp:DataGrid id=dgDetail runat="server" AutoGenerateColumns="False" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).CreateChildView("tableRelation") %>' DataKeyField="OrderID" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" BackColor="White" CellPadding="4" GridLines="Horizontal" Width="100%" Font-Size="Smaller">
           <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#339966"></SelectedItemStyle>
           <ItemStyle ForeColor="#333333" BackColor="White"></ItemStyle>
           <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#336666"></HeaderStyle>
           <FooterStyle ForeColor="#333333" BackColor="White"></FooterStyle>
           <Columns>
            <asp:BoundColumn Visible="False" DataField="OrderID" ReadOnly="True"></asp:BoundColumn>
            <asp:TemplateColumn HeaderText="产品名称">
             <HeaderStyle Width="250px"></HeaderStyle>
             <ItemTemplate>
              <%# DataBinder.Eval(Container.DataItem, "ProductName") %>
             </ItemTemplate>
            </asp:TemplateColumn>
            <asp:BoundColumn DataField="UnitPrice" HeaderText="单价" DataFormatString="{0:c}"></asp:BoundColumn>
            <asp:BoundColumn DataField="Quantity" HeaderText="数量"></asp:BoundColumn>
            <asp:BoundColumn DataField="Discount" HeaderText="折扣"></asp:BoundColumn>
           </Columns>
           <PagerStyle HorizontalAlign="Center" ForeColor="White" BackColor="#336666" Mode="NumericPages"></PagerStyle>
          </asp:DataGrid></TD>
        </TR>
       </TABLE>
      </ItemTemplate>
     </asp:TemplateColumn>
    </Columns>
   </asp:DataGrid>
  </form>
 </body>
</HTML>
/*****************************************************************************/
源码:
/*****************************************************************************/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace CommonFunction
{
 /// <summary>
 /// DataGridMasterFrom 的摘要说明。
 /// </summary>
 public class DataGridMasterFrom : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DataGrid dgMaster;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   //页面初试化时进行数据绑定
   if(!IsPostBack)
    DataGridDataBind();
  }

  private void DataGridDataBind()
  {
   //定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
   SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
   //创建数据适配器对象
   SqlDataAdapter da = new SqlDataAdapter("Select top 5 * from Orders  select OrderID ,Products.ProductName,[order Details].Unitprice,[order Details].Quantity,[order Details].discount from [order Details],Products where [Order Details].ProductId=Products.ProductID",conn);
   //创建DataSet对象
   DataSet ds = new DataSet();
   try
   {
    //填充数据集
    da.Fill(ds);
    //设定表名字
    ds.Tables[0].TableName = "Orders";
    ds.Tables[1].TableName = "Order Details";
    DataColumn Parent = ds.Tables["Orders"].Columns["OrderID"];
    DataColumn Child  = ds.Tables["Order Details"].Columns["OrderID"];
    DataRelation tableRelation = new DataRelation("tableRelation", Parent, Child, false);
    ds.Relations.Add(tableRelation);
    //进行数据绑定
    dgMaster.DataSource = ds.Tables["Orders"].DefaultView;
    dgMaster.DataBind();
   }
   catch(Exception error)
   {
    Response.Write(error.ToString());
   } 
  }

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

  }
  #endregion
 }
}

posted @ 2005-05-20 09:12  编程入门  阅读(589)  评论(0编辑  收藏  举报