• DataGrid 嵌套 dataGrid 的实现


      1CS:
      2using System;
      3using System.Collections;
      4using System.ComponentModel;
      5using System.Data;
      6using System.Drawing;
      7using System.Web;
      8using System.Web.SessionState;
      9using System.Web.UI;
     10using System.Web.UI.WebControls;
     11using System.Web.UI.HtmlControls;
     12using System.Data.SqlClient;
     13namespace Demo
     14{
     15    /// <summary>
     16    /// WebForm9 的摘要说明。
     17    /// </summary>

     18    public class WebForm9 : System.Web.UI.Page
     19    {
     20        protected System.Web.UI.WebControls.DataGrid dgParent;
     21        
     22    
     23        private void Page_Load(object sender, System.EventArgs e)
     24        {
     25            //DataGrid 中嵌套 DataGrid 的做法及方法
     26
     27            if(!Page.IsPostBack)
     28            {
     29                DataGridBind();
     30            }

     31        }

     32        private void DataGridBind()
     33        {
     34            
     35            //使用datagrid 显示层次数据
     36
     37            //DataRowView view=new DataRowView();
     38 
     39            //view.CreateChildView()
     40
     41            //中的参数是:
     42
     43            //System.Data.DataRelation 的名称的字符串
     44            
     45            //datasource='<%# ((DataRowView)Container.DataItem).CreateChildView("tableRelation") %>' 
     46            
     47            //这句话的解释是:
     48            
     49            //具有指定的 System.Data.DataRelation 名称的子级System.Data.DataTable 的System.Data.DataView 的一个视图
     50
     51
     52            
     53            SqlConnection conn = new SqlConnection("server=.;user=sa;pwd=;database=pubs");
     54            
     55            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM authors; SELECT * FROM titleauthor",conn);
     56            
     57            DataSet ds = new DataSet();
     58
     59            try
     60            {
     61                //填充数据集
     62                da.Fill(ds);
     63
     64                //设定表名字
     65                ds.Tables[0].TableName = "authors";
     66
     67                ds.Tables[1].TableName = "titleauthor";
     68
     69                DataColumn Parent = ds.Tables["authors"].Columns["au_id"];//父表id的集合
     70
     71                DataColumn Child  = ds.Tables["titleauthor"].Columns["au_id"]; //子表id的集合 
     72
     73                // 父主表的id必须是关联的
     74
     75                DataRelation tableRelation = new DataRelation("tableRelation", Parent, Child, false);//表示两个table之间的关系,false 是不设置约束
     76    
     77                ds.Relations.Add(tableRelation);
     78
     79                //进行数据绑定
     80                dgParent.DataSource = ds.Tables["authors"].DefaultView;
     81
     82                dgParent.DataBind();
     83
     84            }

     85            catch(System.Data.SqlClient.SqlException e)
     86            {
     87                throw new Exception(e.Message);
     88                
     89            }
        
     90            finally
     91            {
     92                conn.Close();
     93                conn.Dispose();
     94                da.Dispose();
     95                ds.Dispose();
     96            }

     97        
     98        }

     99
    100        Web 窗体设计器生成的代码
    120    }

    121}

    122
    123
    124HTML:
    125<%@ Page language="c#" Codebehind="WebForm9.aspx.cs" AutoEventWireup="false" Inherits="Demo.WebForm9" %>
    126<%@ Import NameSpace="System.Data"%>
    127<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    128<html>
    129    <head>
    130        <title></title>
    131        <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
    132        <meta content="C#" name="CODE_LANGUAGE">
    133        <meta content="JavaScript" name="vs_defaultClientScript">
    134        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    135    </head>
    136    <body>
    137        <form id="form1" method="post" runat="server">
    138            <asp:datagrid id="dgParent" runat="server" autogeneratecolumns="False" cellpadding="4">
    139                <columns>
    140                    <asp:templatecolumn>
    141                        <itemtemplate>
    142                            <table cellspacing="0" cellpadding="0" width="100%" border="0">
    143                                <tr>
    144                                    <td bgcolor="#3399ff"><b>根目录:ID
    145                                            <%# DataBinder.Eval(Container.DataItem, "au_id"%>
    146                                        </b>
    147                                    <td bgcolor="#3399ff">
    148                                        名称:
    149                                        <%# ((DataRowView)Container.DataItem)["au_lname"%>
    150                                    </td>
    151                                    </td>
    152                                </tr>
    153                                <tr>
    154                                    <td>
    155                                        <asp:datagrid id="dgChild" runat="server" autogeneratecolumns="False" datasource='<%# ((DataRowView)Container.DataItem).CreateChildView("tableRelation") %>' datakeyfield="au_id" width="100%" >
    156                                            <columns>
    157                                                <asp:boundcolumn datafield="au_id" headertext="ID"></asp:boundcolumn>
    158                                                <asp:boundcolumn datafield="title_id" headertext="子目录"></asp:boundcolumn>
    159                                            </columns>
    160                                        </asp:datagrid></td>
    161                                </tr>
    162                            </table>
    163                        </itemtemplate>
    164                    </asp:templatecolumn>
    165                </columns>
    166                <pagerstyle horizontalalign="Left" forecolor="#003399" backcolor="#99CCCC" mode="NumericPages"></pagerstyle>
    167            </asp:datagrid></form>
    168    </body>
    169</html>
    170
    171
    172
  • 相关阅读:
    P6585 中子衰变
    [APIO2020]有趣的旅途
    CF1354F Summoning Minions
    CF1361C Johnny and Megan's Necklace
    CF1368E Ski Accidents
    CF1458C Latin Square
    CF1368F Lamps on a Circle
    用户和组的管理
    Windows命令
    1
  • 原文地址:https://www.cnblogs.com/suneryong/p/797915.html
Copyright © 2020-2023  润新知