GridView模版列嵌套GirdView显示主从表数据

当需要在一个列表中显示主从表(例如部门-人员的信息),在asp.net1.1中我们可能会使用DataGrid模版列嵌套DataGrid的方法实现,然而,处理模版列里的DataGrid的翻页、排序、编辑等功能时都比较麻烦。在asp.net2.0中,配合DataSource控件的使用让这个问题变得非常简单!
 
<% @ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_GirdView.aspx.cs" Inherits="GridSamples_GridView_GirdView"  %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > 无标题页 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< asp:GridView  ID ="GridView1"  runat ="server"  AutoGenerateColumns ="False"  DataKeyNames ="deptid"
            DataSourceID
="AccessDataSource1"  AllowPaging ="True"  AllowSorting ="True"  PageSize ="2"  OnRowDataBound ="GridView1_RowDataBound" >
            
< Columns >
                
< asp:BoundField  DataField ="deptid"  HeaderText ="部门编号"  InsertVisible ="False"  ReadOnly ="True"
                    SortExpression
="deptid"   />
                
< asp:BoundField  DataField ="deptname"  HeaderText ="部门名称"  SortExpression ="deptname"   />
                
< asp:BoundField  DataField ="deptremark"  HeaderText ="备注"  SortExpression ="deptremark"   />
                
< asp:TemplateField  HeaderText ="人员信息" >
                    
< ItemTemplate >
                        
< asp:GridView  ID ="GridView2"  runat ="server"  AutoGenerateColumns ="False"  DataKeyNames ="id"
                            DataSourceID
="AccessDataSource2"  AllowPaging ="True"  AllowSorting ="True"  PageSize ="5" >
                            
< Columns >
                                
< asp:BoundField  DataField ="id"  HeaderText ="人员编号"  InsertVisible ="False"  ReadOnly ="True"
                                    SortExpression
="id"   />
                                
< asp:BoundField  DataField ="name"  HeaderText ="姓名"  SortExpression ="name"   />
                                
< asp:BoundField  DataField ="sex"  HeaderText ="性别"  SortExpression ="sex"   />
                            
</ Columns >
                            
< PagerSettings  FirstPageText ="首页"  LastPageText ="末页"  Mode ="NextPreviousFirstLast"
                NextPageText
="下一页"  PreviousPageText ="上一页"   />
                        
</ asp:GridView >
                        
< asp:AccessDataSource  ID ="AccessDataSource2"  runat ="server"  DataFile ="~/App_Data/test.mdb"
                            SelectCommand
="SELECT [id], [name], [sex], [deptid] FROM [employees] WHERE ([deptid] = ?)" >
                            
< SelectParameters >
                                
< asp:Parameter  Name ="deptid"  Type ="Int32"   />
                            
</ SelectParameters >
                        
</ asp:AccessDataSource >< br >
                    
</ ItemTemplate >
                
</ asp:TemplateField >
                
            
</ Columns >
            
< PagerSettings  FirstPageText ="首页"  LastPageText ="末页"
                NextPageText
="下一页"  PreviousPageText ="上一页"   />
        
</ asp:GridView >
        
< asp:AccessDataSource  ID ="AccessDataSource1"  runat ="server"  DataFile ="~/App_Data/test.mdb"
            SelectCommand
="SELECT [deptid], [deptname], [deptremark], [createdate] FROM [departments]" >
        
</ asp:AccessDataSource >
    
    
</ div >
    
</ form >
</ body >
</ html >
 1 using  System;
 2 using  System.Data;
 3 using  System.Configuration;
 4 using  System.Collections;
 5 using  System.Web;
 6 using  System.Web.Security;
 7 using  System.Web.UI;
 8 using  System.Web.UI.WebControls;
 9 using  System.Web.UI.WebControls.WebParts;
10 using  System.Web.UI.HtmlControls;
11
12 public  partial  class  GridSamples_GridView_GirdView : System.Web.UI.Page
13 {
14    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
15    {
16        if (e.Row.RowIndex > -1)
17        {
18            AccessDataSource accessDS = e.Row.FindControl("AccessDataSource2"as AccessDataSource;
19            accessDS.SelectParameters["deptid"].DefaultValue = e.Row.Cells[0].Text;
20        }

21    }

22}
 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值