Bruce Xiao 的程序生活

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

<asp:datagrid id="DataGrid1" ShowHeader =false AutoGenerateColumns =true runat="server" DataKeyField="OrderId" OnItemDataBound="ItemDB">
 
     <Columns>
 
          <asp:TemplateColumn >
 
               <ItemTemplate>
 
                    <b><%#DataBinder.Eval(Container.DataItem,"OrderId")%></b>
 
               </ItemTemplate>
 
          </asp:TemplateColumn>
 
     </Columns>
 
</asp:datagrid> 




SqlConnection cn;
 
SqlDataAdapter da;
 
DataSet ds;
 
private void Page_Load(object sender, System.EventArgs e)
 
{
 
     // Put user code to initialize the page here
 
     cn= new SqlConnection ("Server=localhost;uid=sa;pwd=;database=northwind");
 
     if (!Page.IsPostBack)
 
     {
 
          da= new SqlDataAdapter ("SELECT orderid FROM orders ", cn);
 
          ds= new DataSet ();
 
          da.Fill (ds, "Orders");
 
          DataGrid1.DataSource = ds;
 
          DataGrid1.DataBind ();
 
     }
 
}
 

 
protected void ItemDB(Object sender,DataGridItemEventArgs e )
 
{
 
     if ((e.Item.ItemType == ListItemType.Item)||(e.Item.ItemType == ListItemType.AlternatingItem ))
 
     {
 
          DataGrid dgDetails = new DataGrid();
 
          int orderid =(int) ((DataRowView)e.Item.DataItem)["OrderID"] ;
 
          dgDetails.DataSource = GetOrderDetails(orderid );
 
          dgDetails.DataBind();
 
          e.Item.Cells[1].Controls.Add(dgDetails);
 
     }
 
}
 

 
DataSet GetOrderDetails(int id )
 
{
 
     da= new SqlDataAdapter ("SELECT * FROM [Order Details] where orderid= " + id, cn);
 
     ds= new DataSet ();
 
     da.Fill (ds, "OrderDetails");
 
     return ds;
 
}
 

posted on 2006-11-01 16:06  Bruce Xiao  阅读(229)  评论(0编辑  收藏  举报