我是菜鸟!贴一篇基础文章!
Test.Aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient ;
namespace bns.Test
{
/// <summary>
/// test 的摘要说明。
/// </summary>
public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public string connstring="server=.;uid=sa;pwd=tianpeng;database=northwind";
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection OrdersConnection =new SqlConnection (connstring);
OrdersConnection.Open ();
SqlDataAdapter OrderAdapter =new SqlDataAdapter("Select * from Orders; select * from [Order Details]", OrdersConnection);
DataSet ds=new DataSet();
OrderAdapter.Fill (ds);
ds.Tables [0].TableName ="Orders";
ds.Tables [1].TableName ="OrderDetails";
DataColumn Parent;
Parent=ds.Tables["Orders"].Columns["OrderId"];
DataColumn Child;
Child=ds.Tables["OrderDetails"].Columns ["OrderId"];
DataRelation OrderRelation=new DataRelation("OrderRelation",Parent,Child,false);
ds.Relations.Add(OrderRelation);
DataGrid1.DataSource =ds.Tables["Orders"].DefaultView;
DataGrid1.DataBind ();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Test.aspx
<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false" Inherits="bns.Test.test" %>
<%@ Import NameSpace="System.Data"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" BorderColor="#0099FF" runat="server" ShowHeader="False" Width="470px"
CellPadding="0" CellSpacing="0" AutoGenerateColumns="False" BorderWidth="2px">
<Columns>
<asp:TempLateColumn>
<ItemTemplate>
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tr>
<td bgcolor="#ff6600"><b>订单号<%# DataBinder.Eval(Container.DataItem,"orderid")%></b></td>
</tr>
<tr>
<td align="right">
<asp:DataGrid ID =DataGrid2 Runat =server AutoGenerateColumns =false BorderColor="#33FF33" DataKeyField="OrderID"
DataSource='<%# ((DataRowView)Container.DataItem).CreateChildView("OrderRelation") %>'>
<columns>
<asp:BoundColumn Visible="False" DataField="OrderID" ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="产品名称">
<HeaderStyle Width="300px"></HeaderStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"ProductID")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="UnitPrice" HeaderText="单价"></asp:BoundColumn>
<asp:BoundColumn DataField="Quantity" HeaderText="数量"></asp:BoundColumn>
<asp:BoundColumn DataField="Discount" HeaderText="折扣"></asp:BoundColumn>
</columns>
</asp:DataGrid>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TempLateColumn>
</Columns>
</asp:DataGrid></FONT>
</form>
</body>
</HTML>