CSDN博客

img bccu

DataGrid的使用:(二)、利用ASP.NET DataGrid显示主次关系的数据

发表于2004/10/30 10:49:00  930人阅读

分类: 学习笔记

利用ASP.NET DataGrid显示主次关系的数据

在写论坛的过程中,因为在显示各个版区的时候需要显示主次关系的数据,于是嵌套了DataGrid,其他的数据控件应也适用。

aspx里写:
<HTML>
 <HEAD>
  <title>WebForm1</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="FrmDataGrid" method="post" runat="server">
   <P align="center"><FONT face="宋体"></FONT>&nbsp;</P>
   <asp:DataGrid id="DataGrid1" BorderColor="#0099FF" runat="server" ShowHeader="False" Width="470px"
    CellPadding="0" AutoGenerateColumns="False" BorderWidth="2px" style="Z-INDEX: 101; LEFT: 0px; POSITION: absolute; TOP: 0px">
    <Columns>
     <asp:TemplateColumn>
      <ItemTemplate>
       <TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
        <TR>
         <TD bgColor="#ff6600"><B>订单号:
           <%# DataBinder.Eval(Container.DataItem, "OrderID") %>
          </B>
         </TD>
        </TR>
        <TR>
         <TD align="right">
          <asp:DataGrid id=DataGrid2 runat="server" AutoGenerateColumns="False" BorderColor="#33FF33" DataSource=' <%# DataBinder.Eval(Container.DataItem, "OrderRelation") %>' DataKeyField="OrderID">
           <HeaderStyle Font-Bold="True" ForeColor="#CC0066" BackColor="#FFCCFF"></HeaderStyle>
           <Columns>
            <asp:BoundColumn Visible="False" DataField="OrderID" ReadOnly="True"></asp:BoundColumn>
            <asp:TemplateColumn HeaderText="产品名称">
             <HeaderStyle Width="300px"></HeaderStyle>
             <ItemTemplate>
              <%# DataBinder.Eval(Container.DataItem, "ProductName") %>
             </ItemTemplate>
            </asp:TemplateColumn>
            <asp:BoundColumn DataField="UnitPrice" HeaderText="单价"></asp:BoundColumn>
            <asp:BoundColumn DataField="Quantity" HeaderText="数量"></asp:BoundColumn>
            <asp:BoundColumn DataField="Discount" HeaderText="折扣"></asp:BoundColumn>
           </Columns>
          </asp:DataGrid>
         </TD>
        </TR>
       </TABLE>
      </ItemTemplate>
     </asp:TemplateColumn>
    </Columns>
   </asp:DataGrid>
  </form>
  <P align="center"><FONT face="宋体"></FONT></P>
 </body>
</HTML>

aspx.cs里写:

//得到订单和订单详细信息
SqlConnection conn=new SqlConnection ("Server=tyhy06;Database=northwind;user=sa");
SqlDataAdapter sqldap=new SqlDataAdapter ("Select * 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 ();
sqldap.Fill (ds);
//设定表名字
ds.Tables[0].TableName ="Orders";
ds.Tables[1].TableName ="Order Details";

//在基于OrderID字段的两个表之间建立父子关系
DataColumn parent=ds.Tables["Orders"].Columns["OrderID"];
DataColumn child=ds.Tables["Order Details"].Columns["OrderID"];

DataRelation OrderRelation=new DataRelation ("OrderRelation",parent,child);
//
//为DataSet添加关系
ds.Relations .Add (OrderRelation);

//把DataGrid绑定到Orders表
this.DataGrid1.DataSource =ds.Tables["Orders"].DefaultView ;
this.DataGrid1.DataBind ();

阅读全文
0 0

相关文章推荐

img
取 消
img