当前位置:   article > 正文

datagrid嵌套和资源消耗

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生成的,懒的写。

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


None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  test1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// WebForm2 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
InBlock.gif            
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
InBlock.gif            
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.dataSet1 = new System.Data.DataSet();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.dataSet1)).BeginInit();
InBlock.gif            
// 
InBlock.gif            
// sqlDataAdapter1
InBlock.gif            
// 
InBlock.gif
            this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
InBlock.gif            
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
InBlock.gif            
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                                                                      
new System.Data.Common.DataTableMapping("Table""Employees"new System.Data.Common.DataColumnMapping[] dot.gif{
ExpandedSubBlockEnd.gif                                                                                                                                                                                                                   
new System.Data.Common.DataColumnMapping("EmployeeID""EmployeeID")}
)}
);
InBlock.gif            
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
InBlock.gif            
// 
InBlock.gif            
// sqlDeleteCommand1
InBlock.gif            
// 
InBlock.gif
            this.sqlDeleteCommand1.CommandText = "DELETE FROM Employees WHERE (EmployeeID = @Original_EmployeeID)";
InBlock.gif            
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
InBlock.gif            
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_EmployeeID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "EmployeeID", System.Data.DataRowVersion.Original, null));
InBlock.gif            
// 
InBlock.gif            
// sqlConnection1
InBlock.gif            
// 
InBlock.gif
            this.sqlConnection1.ConnectionString = "workstation id=李征;packet size=4096;user id=fa;pwd=fa;data source=李征;persist secur" +
InBlock.gif                
"ity info=False;initial catalog=Northwind";
InBlock.gif            
// 
InBlock.gif            
// sqlInsertCommand1
InBlock.gif            
// 
InBlock.gif
            this.sqlInsertCommand1.CommandText = "INSERT INTO Employees() VALUES (); SELECT EmployeeID FROM Employees WHERE (Employ" +
InBlock.gif                
"eeID = @@IDENTITY)";
InBlock.gif            
this.sqlInsertCommand1.Connection = this.sqlConnection1;
InBlock.gif            
// 
InBlock.gif            
// sqlSelectCommand1
InBlock.gif            
// 
InBlock.gif
            this.sqlSelectCommand1.CommandText = "SELECT EmployeeID FROM Employees";
InBlock.gif            
this.sqlSelectCommand1.Connection = this.sqlConnection1;
InBlock.gif            
// 
InBlock.gif            
// sqlUpdateCommand1
InBlock.gif            
// 
InBlock.gif
            this.sqlUpdateCommand1.CommandText = "UPDATE Employees SET WHERE (EmployeeID = @Original_EmployeeID); SELECT EmployeeID" +
InBlock.gif                
" FROM Employees WHERE (EmployeeID = @EmployeeID)";
InBlock.gif            
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
InBlock.gif            
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_EmployeeID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "EmployeeID", System.Data.DataRowVersion.Original, null));
InBlock.gif            
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@EmployeeID", System.Data.SqlDbType.Int, 4"EmployeeID"));
InBlock.gif            
// 
InBlock.gif            
// dataSet1
InBlock.gif            
// 
InBlock.gif
            this.dataSet1.DataSetName = "NewDataSet";
InBlock.gif            
this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
InBlock.gif            
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
InBlock.gif            
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.dataSet1)).EndInit();
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private DataView BindGrid2(int EmployeeID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection myconn
=new SqlConnection();
InBlock.gif            myconn.ConnectionString
="workstation id=李征;packet size=4096;user id=fa;pwd=fa;data source=李征;persist secur" +
InBlock.gif                
"ity info=False;initial catalog=Northwind";
InBlock.gif
InBlock.gif            SqlDataAdapter myda
=new SqlDataAdapter("select TerritoryID from EmployeeTerritories where EmployeeID="+EmployeeID.ToString(),myconn);
InBlock.gif
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif
InBlock.gif            myda.Fill(ds);
InBlock.gif
InBlock.gif            
return ds.Tables[0].DefaultView;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataGrid dgt 
= (DataGrid)e.Item.FindControl("DataGrid2");
InBlock.gif
InBlock.gif                dgt.DataSource
=BindGrid2(int.Parse(e.Item.Cells[0].Text.ToString()));
InBlock.gif                dgt.DataBind();
InBlock.gif
InBlock.gif                
//Label lb=(Label)e.Item.FindControl("l1");
InBlock.gif
InBlock.gif                
//lb.Text="111";
InBlock.gif

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


挑重点看看吧。。。

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

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

转载于:https://www.cnblogs.com/jpwar/archive/2005/09/14/236451.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/639498
推荐阅读
相关标签
  

闽ICP备14008679号