首页  ·  知识 ·  云计算
GridView模版列嵌套GirdView显示主从表数据
xiongeee  http://www.cnblogs.com/xiongeee/  综合  编辑:dezai  图片来源:网络
当需要在一个列表中显示主从表(例如部门-人员的信息),在asp.net1.1中我们可能会使用DataGrid模版列嵌套DataGrid的方法实现,然而,处理模版列里的DataGri

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

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_GirdView.aspx.cs" Inherits="GridSamples_GridView_GirdView" %>

http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

http://www.w3.org/1999/xhtml" >

    无标题页


   
   


                    DataSourceID="AccessDataSource1" AllowPaging="True" AllowSorting="True" PageSize="2" OnRowDataBound="GridView1_RowDataBound">
           
                                    SortExpression="deptid" />
               
               
               
                   
                                                    DataSourceID="AccessDataSource2" AllowPaging="True" AllowSorting="True" PageSize="5">
                           
                                                                    SortExpression="id" />
                               
                               
                           

                                            NextPageText="下一页" PreviousPageText="上一页" />
                       

                                                    SelectCommand="SELECT [id], [name], [sex], [deptid] FROM WHERE ([deptid] = ?)">
                           
                               
                           

                       


                   

               

               
           

                            NextPageText="下一页" PreviousPageText="上一页" />
       

                    SelectCommand="SELECT [deptid], [deptname], [deptremark], [createdate] FROM [departments]">
       

   
   

   


 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Collections;
 5using System.Web;
 6using System.Web.Security;
 7using System.Web.UI;
 8using System.Web.UI.WebControls;
 9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public 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}
只需要上面几行简单的代码便可以实现。

本文作者:xiongeee 来源:http://www.cnblogs.com/xiongeee/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读