ASP.net技巧:DataList、Gridview主从表明细显示

图1.使用DataList显示主从表明细 
图2.使用Gridview显示主从表明细
 
数据库为SQL Server实例数据库Northwind,实现按照订单编号,统计显示出订单详情。
          一构建Northwind库中Orders订单表以及Order Details订单详细表在DataSet中的表关系:
InBlock.gif public class DataAccess
{
    public DataSet Tablerelation() 
InBlock.gif        { 
InBlock.gif                string strCon = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
InBlock.gif                 
InBlock.gif                //构建连接 
InBlock.gif                SqlConnection con = new SqlConnection(strCon); 
InBlock.gif            
InBlock.gif                //多个表来源于相同数据库我们可以讲sql语句一起书写,但需要注意的是,必须在语句间用空格隔开 
InBlock.gif                SqlDataAdapter da = new SqlDataAdapter("select    * from orders    select * from [order details]",con); 
InBlock.gif                DataSet ds = new DataSet(); 
InBlock.gif                da.Fill(ds); 
InBlock.gif 
InBlock.gif                //我们也可以修改默认生成的(Table、Table1、……)表名 
InBlock.gif                ds.Tables [0].TableName="orders"
InBlock.gif                ds.Tables[1].TableName = "orderDetails"
InBlock.gif 
InBlock.gif                //找到两个表中相关联的列 
InBlock.gif                DataColumn father = ds.Tables["orders"].Columns["OrderID"]; 
InBlock.gif                DataColumn son = ds.Tables["orderDetails"].Columns["OrderID"]; 
InBlock.gif 
InBlock.gif                //给两个列,建立名为tablerelation的关系 
InBlock.gif                DataRelation r = new DataRelation("tablerelation", father, son); 
InBlock.gif 
InBlock.gif                //将表关系添加到数据集中 
InBlock.gif                ds.Relations.Add(r); 
InBlock.gif                return ds; 
InBlock.gif        } 
}
wef.config中的连接字符串为:
InBlock.gif<connectionStrings> 
InBlock.gif  <add name="NorthwindConnectionString" connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/> 
InBlock.gif  </connectionStrings>
接下来我们来布置控件的界面:
一DataList:
    给DataList1的项中添加一个一行二列的表,第一个单元格中绑定订单编号,第二个单元格中再加入一个DataList2作为此订单编号的订单详细,在DataList2的项中添加一个一行四列的表,单元格中绑定订单详细表中的字段名。给DataList1的头模版中加入一个一行五列的表,分别写入图片所示的列说明,布局源码如下:
InBlock.gif    <asp:DataList ID="DataList1" runat="server" BackColor="White"    
InBlock.gif                        BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"    
InBlock.gif                        GridLines="Both"
InBlock.gif                        <ItemStyle ForeColor="#000066" /> 
InBlock.gif                        <SelectedItemStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> 
InBlock.gif                        <HeaderTemplate> 
InBlock.gif                                <table class="style1" > 
InBlock.gif                                        <tr> 
InBlock.gif                                                <td width="70"
InBlock.gif                                                        订单编号</td> 
InBlock.gif                                                <td width="70"
InBlock.gif                                                        商品编号</td> 
InBlock.gif                                                <td width="70"
InBlock.gif                                                        单价</td> 
InBlock.gif                                                <td width="70"
InBlock.gif                                                        数量</td> 
InBlock.gif                                                <td width="70"
InBlock.gif                                                        折扣</td> 
InBlock.gif                                        </tr> 
InBlock.gif                                </table> 
InBlock.gif                        </HeaderTemplate> 
InBlock.gif                        <FooterStyle BackColor="White" ForeColor="#000066" /> 
InBlock.gif                        <ItemTemplate> 
InBlock.gif                                <table class="style1"
InBlock.gif                                        <tr> 
InBlock.gif                                                <td width="70"
InBlock.gif                                                        <%#Eval("OrderID") %></td> 
InBlock.gif                                                <td> 
InBlock.gif                                                        <asp:DataList ID="DataList2" runat="server"    
InBlock.gif                                                                DataSource ='<%# ((System.Data.DataRowView)Container.DataItem).CreateChildView("tablerelation") %>'    
InBlock.gif                                                                Width ="100%" BackColor="White" BorderColor="#CC9966" BorderStyle="None"    
InBlock.gif                                                                BorderWidth="1px" CellPadding="4" GridLines="Both"
InBlock.gif                                                                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> 
InBlock.gif                                                                <ItemTemplate> 
InBlock.gif                                                                        <table class="style1"
InBlock.gif                                                                                <tr> 
InBlock.gif                                                                                        <td width="70"
InBlock.gif                                                                                             <%#Eval("productID") %></td> 
InBlock.gif                                                                                        <td width="70"
InBlock.gif                                                                                             <%#Eval("Unitprice") %></td> 
InBlock.gif                                                                                        <td width="70"
InBlock.gif                                                                                                 <%#Eval("Quantity") %></td> 
InBlock.gif                                                                                        <td width="70"
InBlock.gif                                                                                             <%#Eval("Discount") %></td> 
InBlock.gif                                                                                </tr> 
InBlock.gif                                                                        </table> 
InBlock.gif                                                                </ItemTemplate> 
InBlock.gif                                                                <ItemStyle BackColor="White" ForeColor="#330099" /> 
InBlock.gif                                                                <SelectedItemStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> 
InBlock.gif                                                                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> 
InBlock.gif                                                        </asp:DataList> 
InBlock.gif                                                </td> 
InBlock.gif                                        </tr> 
InBlock.gif                                </table> 
InBlock.gif                        </ItemTemplate> 
InBlock.gif                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> 
InBlock.gif                </asp:DataList>
    其中用黄色背景加粗显示的为DataList2的数据源将是根据该项中的订单编号,在表关系名“tablerelation”中找到对应的订单详情,创建一个视图。
     DataList1的数据源将绑定我们以及构建好的DataSet,代码如下:
InBlock.gif    protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                if (!IsPostBack) 
InBlock.gif                { 
InBlock.gif                     
InBlock.gif                        Bind(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif        //绑定控件显示 
InBlock.gif        void Bind() 
InBlock.gif        { 
InBlock.gif                DataAccess da = new DataAccess(); 
InBlock.gif                DataSet ds = da.Tablerelation(); 
InBlock.gif                DataList1.DataSource = ds.Tables[0]; 
InBlock.gif                DataList1.DataBind(); 
InBlock.gif 
InBlock.gif                GridView2.DataSource = ds.Tables[0]; 
InBlock.gif                GridView2.DataBind(); 
InBlock.gif        }
二、使用Gridview的方式类似,首先将Gridview2的自动生成列AutoGenerateColumns="False"改为false,然后在Gridview2手动加入自定义项TemplateField中编辑模版,加入二行一列的表格,第一行绑定订单编号,第二行加入Gridview3,用于显示订单明细,自动生成列AutoGenerateColumns="False"改为false,加入自定义绑定的四个项目,如 <asp:BoundField DataField="productID" HeaderText="商品编号" />,源码如下:
InBlock.gif <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"    
InBlock.gif                        CellPadding="4" ForeColor="#333333" GridLines="None" > 
InBlock.gif                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
InBlock.gif                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
InBlock.gif                        <Columns> 
InBlock.gif                                <asp:TemplateField> 
InBlock.gif                                        <ItemTemplate> 
InBlock.gif                                                <table class="style1"
InBlock.gif                                                        <tr> 
InBlock.gif                                                                <td> 
InBlock.gif                                                                     订单编号:    <%#Eval("OrderID") %></td> 
InBlock.gif                                                        </tr> 
InBlock.gif                                                        <tr> 
InBlock.gif                                                                <td> 
InBlock.gif                                                                        <asp:GridView ID="GridView3" runat="server"     DataSource ='<%# ((System.Data.DataRowView)Container.DataItem).CreateChildView("order") %>' DataMember="orderID" AutoGenerateColumns="False"
InBlock.gif                                                                         <Columns> 
InBlock.gif                                                                            
InBlock.gif                                                                                <asp:BoundField DataField="productID" HeaderText="商品编号" /> 
InBlock.gif                                                                                                <asp:BoundField DataField="Unitprice" HeaderText="商品单价" /> 
InBlock.gif                                                                                                <asp:BoundField DataField="Quantity" HeaderText="商品数量" /> 
InBlock.gif                                                                                                <asp:BoundField DataField="Discount" HeaderText="折扣" /> 
InBlock.gif                                                                                
InBlock.gif                                                                                </Columns> 
InBlock.gif                                                                                 
InBlock.gif                                                                        </asp:GridView> 
InBlock.gif                                                                         
InBlock.gif                                                                </td> 
InBlock.gif                                                        </tr> 
InBlock.gif                                                </table> 
InBlock.gif                                        </ItemTemplate> 
InBlock.gif                                </asp:TemplateField> 
InBlock.gif                        </Columns> 
InBlock.gif                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
InBlock.gif                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
InBlock.gif                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
InBlock.gif                        <EditRowStyle BackColor="#999999" /> 
InBlock.gif                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
InBlock.gif                </asp:GridView>

Gridview3的数据源的绑定方式如上原理。

本文转自叶子文文博客51CTO博客,原文链接http://blog.51cto.com/leafwf/188602如需转载请自行联系原作者


叶子文文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值