62,072
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="OrderID" />
<asp:BoundField DataField="LinkMan" HeaderText="LinkMan" />
<asp:BoundField DataField="SubmitTime" HeaderText="SubmitTime" />
<asp:TemplateField HeaderText="Detail">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" OnRowDataBound="GridView2_RowDataBound">
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
/// <summary>
/// 订单
/// </summary>
public class Order
{
public string OrderID { set; get; }
public string LinkMan { set; get; }
public DateTime SubmitTime { set; get; }
}
/// <summary>
/// 商品
/// </summary>
public class Goods
{
public string OrderID { set; get; }
public string GoodsID { set; get; }
public int Amount { set; get; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.GridView1.DataSource = this.GetOrderData();
this.GridView1.DataBind();
}
}
/// <summary>
/// 获取订单信息
/// </summary>
/// <returns>订单列表</returns>
public List<Order> GetOrderData()
{
List<Order> orderList = new List<Order>()
{
new Order(){ OrderID="s001", LinkMan="张三", SubmitTime=DateTime.Now},
new Order(){ OrderID="s002", LinkMan="李四", SubmitTime=DateTime.Now.AddDays(-1)},
new Order(){ OrderID="s003", LinkMan="王五", SubmitTime=DateTime.Now.AddHours(-8)},
new Order(){ OrderID="s004", LinkMan="赵六", SubmitTime=DateTime.Now.AddHours(-6.5)},
new Order(){ OrderID="s005", LinkMan="小七", SubmitTime=DateTime.Now.AddDays(-2)}
};
return orderList;
}
/// <summary>
/// 根据订单号获取商品信息
/// </summary>
/// <param name="OrderID">订单编号</param>
/// <returns>商品信息列表</returns>
public List<Goods> GetGoodsData(string OrderID)
{
List<Goods> goodsList = new List<Goods>()
{
new Goods{ OrderID="s001", GoodsID="SC001", Amount=1},
new Goods{ OrderID="s001", GoodsID="SC002", Amount=1},
new Goods{ OrderID="s001", GoodsID="SC003", Amount=6},
new Goods{ OrderID="s002", GoodsID="SC001", Amount=1},
new Goods{ OrderID="s002", GoodsID="SC004", Amount=10},
new Goods{ OrderID="s002", GoodsID="SC005", Amount=1},
new Goods{ OrderID="s005", GoodsID="SC001", Amount=1},
new Goods{ OrderID="s005", GoodsID="SC005", Amount=1},
new Goods{ OrderID="s005", GoodsID="SC007", Amount=1},
new Goods{ OrderID="s005", GoodsID="SC008", Amount=2},
new Goods{ OrderID="s005", GoodsID="SC004", Amount=3},
new Goods{ OrderID="s005", GoodsID="SC009", Amount=1},
};
return goodsList.FindAll(delegate(Goods g) { return g.OrderID == OrderID; });
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)
{
GridView GridView2 = (GridView)e.Row.FindControl("GridView2");
string OrderID = e.Row.Cells[0].Text;
List<Goods> goodsList = this.GetGoodsData(OrderID);
GridView2.DataSource = goodsList;
GridView2.DataBind();
}
}
}
//第二层GridView的RowDataBound事件
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)
{
if (Convert.ToInt32(e.Row.Cells[2].Text)>=5)
{
//订单中商品数量不小于5的显示红色
e.Row.Cells[2].Attributes.Add("style","background:red;");
}
}
}
}
protected void gvOrder_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvDetail = e.Row.FindControl("gvDetail") as GridView;
if (gvDetail != null)
{
gvDetail.DataSource = GetDetailsByOrderId(gvOrder.DataKeys[e.Row.RowIndex].Value.ToString());
gvDetail.DataBind();
}
}
<!--第一层GridView开始-->
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns=false runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Container.DataItem.ToString()%>
<!--第二层GridView开始-->
<asp:GridView ID="GridView2" OnRowDataBound="GridView2_RowDataBound" AutoGenerateColumns=false runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Container.DataItem.ToString()%>
<!--第三层GridView开始-->
<asp:GridView ID="GridView3" AutoGenerateColumns=false runat=server>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Container.DataItem.ToString()%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<!--第三层GridView 结束-->
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<!--第二层GridView结束-->
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<!--第一层GridView结束-->
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = Al("GridView1Item:");
this.GridView1.DataBind();
}
//DataSource
protected ArrayList Al(string GetStr)
{
ArrayList al = new ArrayList();
for(int i=0;i<2;i++)
{
al.Add(GetStr+i.ToString());
}
return al;
}
//GridView1_RowDataBound
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)e.Row.FindControl("GridView2");
gv.DataSource = Al("GridView2Item:");
gv.DataBind();
}
}
//GridView2_RowDataBound
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)e.Row.FindControl("GridView3");
gv.DataSource = Al("GridView3Item:");
gv.DataBind();
}
}
}