datagrid嵌套和资源消耗

datagrid嵌套的文章很多了。

我这里参考的是以下两篇

http://www.cnblogs.com/lovecherry/archive/2005/04/26/145633.html

http://www.microsoft.com/china/MSDN/library/data/dataAccess/NestGridHierData.mspx

我只是做了datagrid的嵌套,并没有动态添加模板列那么复杂。

最后的代码给大家。有一部分是vs生成的,懒的写。

<% @ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="test1.WebForm2"  %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
< HTML >
    
< HEAD >
        
< title > WebForm2 </ title >
        
< meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
        
< meta  content ="C#"  name ="CODE_LANGUAGE" >
        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
    
</ HEAD >
    
< body >
        
< form  id ="Form1"  method ="post"  runat ="server" >
            
< asp:datagrid  id =DataGrid1  runat ="server"  AutoGenerateColumns ="False"  DataSource ="<%# dataSet1 %>" >
                
< Columns >
                    
< asp:BoundColumn  DataField ="EmployeeID"  HeaderText ="编号" ></ asp:BoundColumn >
                    
< asp:TemplateColumn  HeaderText ="测试列" >
                        
< ItemTemplate >
                            
< asp:DataGrid  id ="DataGrid2"  runat ="server"  AutoGenerateColumns ="False" >
                                
< Columns >
                                    
< asp:BoundColumn  DataField ="TerritoryID"  HeaderText ="编号" ></ asp:BoundColumn >
                                
</ Columns >
                            
</ asp:DataGrid >
                        
</ ItemTemplate >
                    
</ asp:TemplateColumn >
                
</ Columns >
            
</ asp:datagrid >
            
< asp:DataGrid  id ="DataGrid3"  runat ="server"  AutoGenerateColumns ="False" >
                
< Columns >
                    
< asp:BoundColumn  DataField ="TerritoryID"  HeaderText ="标号" ></ asp:BoundColumn >
                
</ Columns >
            
</ asp:DataGrid ></ form >
    
</ body >
</ HTML >


using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Data.SqlClient;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;

namespace  test1
{
    
/// <summary>
    
/// WebForm2 的摘要说明。
    
/// </summary>

    public class WebForm2 : System.Web.UI.Page
    
{
        
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
        
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
        
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
        
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
        
protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
        
protected System.Data.SqlClient.SqlConnection sqlConnection1;
        
protected System.Data.DataSet dataSet1;
        
protected System.Web.UI.WebControls.DataGrid DataGrid3;
        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            if(!IsPostBack)
            
{
                sqlDataAdapter1.Fill(dataSet1);

                DataGrid3.DataSource
=BindGrid2(1);

                DataGrid1.DataBind();
                DataGrid3.DataBind();
            
            }

        }


        
Web 窗体设计器生成的代码

        
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        
{
        }


        
private DataView BindGrid2(int EmployeeID)
        
{
            SqlConnection myconn
=new SqlConnection();
            myconn.ConnectionString
="workstation id=李征;packet size=4096;user id=fa;pwd=fa;data source=李征;persist secur" +
                
"ity info=False;initial catalog=Northwind";

            SqlDataAdapter myda
=new SqlDataAdapter("select TerritoryID from EmployeeTerritories where EmployeeID="+EmployeeID.ToString(),myconn);

            DataSet ds
=new DataSet();

            myda.Fill(ds);

            
return ds.Tables[0].DefaultView;
        }


        
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        
{
            
if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            
{
                DataGrid dgt 
= (DataGrid)e.Item.FindControl("DataGrid2");

                dgt.DataSource
=BindGrid2(int.Parse(e.Item.Cells[0].Text.ToString()));
                dgt.DataBind();

                
//Label lb=(Label)e.Item.FindControl("l1");

                
//lb.Text="111";

            }


        }

    }

}



挑重点看看吧。。。

再说到资源消耗,这么嵌套真有点资源杀手的味道。大部分资源都消耗在对控件的消耗上了。

真难想象再象我引用文章那样的给嵌套的dtatgrid在动态创建列会怎样。

你可能感兴趣的:(datagrid)