GridView模版列嵌套GirdView顯示主從表數據

 當需要在一個列表中顯示主從表(例如部門-人員的信息),在asp.net1.1中我們可能會使用DataGrid模版列嵌套DataGrid的方法實現,然而,處理模版列裏的DataGrid的翻頁、排序、編輯等功能時都比較麻煩。在asp.net2.0中,配合DataSource控件的使用讓這個問題變得非常簡單!
gridview_gridview.JPG 
ExpandedBlockStart.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_GirdView.aspx.cs" Inherits="GridSamples_GridView_GirdView" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>無標題頁</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif    
<div>
None.gif        
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="deptid"
None.gif            DataSourceID
="AccessDataSource1" AllowPaging="True" AllowSorting="True" PageSize="2" OnRowDataBound="GridView1_RowDataBound">
None.gif            
<Columns>
None.gif                
<asp:BoundField DataField="deptid" HeaderText="部門編號" InsertVisible="False" ReadOnly="True"
None.gif                    SortExpression
="deptid" />
None.gif                
<asp:BoundField DataField="deptname" HeaderText="部門名稱" SortExpression="deptname" />
None.gif                
<asp:BoundField DataField="deptremark" HeaderText="備註" SortExpression="deptremark" />
None.gif                
<asp:TemplateField HeaderText="人員信息">
None.gif                    
<ItemTemplate>
None.gif                        
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
None.gif                            DataSourceID
="AccessDataSource2" AllowPaging="True" AllowSorting="True" PageSize="5">
None.gif                            
<Columns>
None.gif                                
<asp:BoundField DataField="id" HeaderText="人員編號" InsertVisible="False" ReadOnly="True"
None.gif                                    SortExpression
="id" />
None.gif                                
<asp:BoundField DataField="name" HeaderText="姓名" SortExpression="name" />
None.gif                                
<asp:BoundField DataField="sex" HeaderText="性別" SortExpression="sex" />
None.gif                            
</Columns>
None.gif                            
<PagerSettings FirstPageText="首頁" LastPageText="末頁" Mode="NextPreviousFirstLast"
None.gif                NextPageText
="下一頁" PreviousPageText="上一頁" />
None.gif                        
</asp:GridView>
None.gif                        
<asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/App_Data/test.mdb"
None.gif                            SelectCommand
="SELECT [id], [name], [sex], [deptid] FROM [employees] WHERE ([deptid] = ?)">
None.gif                            
<SelectParameters>
None.gif                                
<asp:Parameter Name="deptid" Type="Int32" />
None.gif                            
</SelectParameters>
None.gif                        
</asp:AccessDataSource><br>
None.gif                    
</ItemTemplate>
None.gif                
</asp:TemplateField>
None.gif                
None.gif            
</Columns>
None.gif            
<PagerSettings FirstPageText="首頁" LastPageText="末頁"
None.gif                NextPageText
="下一頁" PreviousPageText="上一頁" />
None.gif        
</asp:GridView>
None.gif        
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/test.mdb"
None.gif            SelectCommand
="SELECT [deptid], [deptname], [deptremark], [createdate] FROM [departments]">
None.gif        
</asp:AccessDataSource>
None.gif    
None.gif    
</div>
None.gif    
</form>
None.gif
</body>
None.gif
</html>
None.gif
 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Configuration;
 4None.gifusing System.Collections;
 5None.gifusing System.Web;
 6None.gifusing System.Web.Security;
 7None.gifusing System.Web.UI;
 8None.gifusing System.Web.UI.WebControls;
 9None.gifusing System.Web.UI.WebControls.WebParts;
10None.gifusing System.Web.UI.HtmlControls;
11None.gif
12None.gifpublic partial class GridSamples_GridView_GirdView : System.Web.UI.Page
13ExpandedBlockStart.gif{
14InBlock.gif    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
15ExpandedSubBlockStart.gif    {
16InBlock.gif        if (e.Row.RowIndex > -1)
17ExpandedSubBlockStart.gif        {
18InBlock.gif            AccessDataSource accessDS = e.Row.FindControl("AccessDataSource2"as AccessDataSource;
19InBlock.gif            accessDS.SelectParameters["deptid"].DefaultValue = e.Row.Cells[0].Text;
20ExpandedSubBlockEnd.gif        }

21ExpandedSubBlockEnd.gif    }

22ExpandedBlockEnd.gif}

只需要上面幾行簡單的代碼便可以實現。
發佈了130 篇原創文章 · 獲贊 5 · 訪問量 42萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章