gridview 特技
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="OrderID" SortExpression="OrderID" />
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" />
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" />
<asp:TemplateField HeaderText="OrderDetail">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" DataSource='<%# Bind("OrderDetail") %>' >
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="QueryOrder" TypeName="WebApplication8.OrderDataAccessLayer"></asp:ObjectDataSource>
public class OrderDataAccessLayer
{
public class Order
{
public int OrderID { get; set; }
public string CustomerID { get; set; }
public int EmployeeID { get; set; }
public List<OrderDetail> OrderDetail
{
get
{
return OrderDataAccessLayer.QueryOrderDetail(this.OrderID.ToString());
}
}
}
public class OrderDetail
{
public int OrderID { get; set; }
public int ProductID { get; set; }
public decimal UnitPrice { get; set; }
public decimal Quantity { get; set; }
public decimal Discount { get; set; }
}
public static List<Order> QueryOrder()
{
List<Order> order = new List<Order>();
string ConnStr = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"]
.ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnStr))
{
conn.Open();
string sql = "SELECT top 5 * FROM Orders";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
Order obj = new Order();
obj.OrderID = Convert.ToInt32(dr["OrderID"].ToString());
obj.CustomerID = dr["CustomerID"].ToString();
obj.EmployeeID = Convert.ToInt32(dr["EmployeeID"].ToString());
order.Add(obj);
}
}
return order;
}
public static List<OrderDetail> QueryOrderDetail(string OrderID)
{
List<OrderDetail> orderdetail = new List<OrderDetail>();
string ConnStr = ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"]
.ConnectionString;
using (SqlConnection conn = new SqlConnection(ConnStr))
{
conn.Open();
string sql = "SELECT * FROM [dbo].[Order Details] WHERE OrderID=@OrderID";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter para = new SqlParameter("@OrderID", OrderID);
cmd.Parameters.Add(para);
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
OrderDetail obj = new OrderDetail();
obj.OrderID = Convert.ToInt32(dr["OrderID"].ToString());
obj.ProductID = Convert.ToInt32(dr["ProductID"].ToString());
obj.Quantity = Convert.ToInt32(dr["Quantity"].ToString());
obj.UnitPrice = Convert.ToDecimal(dr["UnitPrice"].ToString());
orderdetail.Add(obj);
}
}
return orderdetail;
}
}