HierarchicalDataGrid.aspx内容
<%@ Page language="c#" Codebehind="HierarchicalDataGrid.aspx.cs" AutoEventWireup="false" Inherits="HierarchicalDataGrid.HierarchicalDataGrid" %> <%@ Import namespace="System.Data"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>?ι??</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content=" http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body> <form ></FONT> </EditItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid></P> </form> </body> </HTML>
HierarchicalDataGrid.aspx.cs内容
using System; using System.Collections; using System.Configuration; using System.Data; using System.Drawing; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Data.SqlClient; namespace HierarchicalDataGrid { public class HierarchicalDataGrid : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DataGrid1 = null; #region " Web Form Designer Generated Code "
// 下面的调用对Web Form设计器来说是必须的 //[System.Diagnostics.DebuggerStepThrough()] override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound); this.Load += new System.EventHandler(this.Page_Load);
} // CODEGEN: 这个方法是为Web Form设计器调用的 // 不要用代码编辑器改变这里. // private void Page_Init(System.Object sender,System.EventArgs e) // { // InitializeComponent(); // } #endregion
// 打开数据库连接 private void Page_Load(System.Object sender,System.EventArgs e) { SqlConnection OrdersConnection = new SqlConnection ("Server=localhost;Database=northwind;user id = sa; password=sa;"); // 得到订单和订单详细信息 SqlDataAdapter OrderAdapter = new SqlDataAdapter ("Select * from Orders; select OrderID ,Products.ProductName,[order Details].Unitprice,[order Details].Quantity,[order Details].discount from [order Details],Products where [Order Details].ProductId=Products.ProductID",OrdersConnection); // 创建并填充DataSet对象 DataSet OrderDataSet = new DataSet (); OrderAdapter.Fill(OrderDataSet); // 设定表名字 OrderDataSet.Tables[0].TableName = "Orders"; OrderDataSet.Tables[1].TableName = "Order Details"; // 在基于OrderID字段的两个表之间建立父子关系 DataColumn Parent = OrderDataSet.Tables["Orders"].Columns["OrderID"]; DataColumn Child = OrderDataSet.Tables["Order Details"].Columns["OrderID"]; DataRelation OrderRelation = new DataRelation ("OrderRelation", Parent, Child, false); // 为DataSet添加关系 OrderDataSet.Relations.Add(OrderRelation); // 把DataGrid绑定到Orders表 DataGrid1.DataSource = OrderDataSet.Tables["Orders"].DefaultView; DataBind(); } private void DataGrid1_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e) { e.Item.Cells[0].BackColor = System.Drawing.Color.Ivory; } } }
|
请发表评论