GridView多行嵌套(整行)

使用用户控件实现,外层GridView和内层GridView个使用一个用户控件。

ucChildGridView.ascx

前台

代码
 1 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucParentGridView.ascx.cs"
 2     Inherits="多层嵌套整行嵌套_ucParentGridView" %>
 3 <link href="~/CSS/Gridview.css" rel="stylesheet" type="text/css" />
 4 <div id="container">
 5     <asp:GridView ID="ParentGridView" AllowPaging="true" BorderColor="Black" OnRowDataBound="ParentGridView_RowDataBound"
 6         runat="server" AutoGenerateColumns="False" Font-Size="12px" Width="530px" AllowSorting="True"
 7         OnRowCreated="ParentGridView_RowCreated">
 8         <Columns>
 9             <asp:BoundField DataField="CategoryID" HeaderText="编号" ReadOnly="True" />
10             <asp:BoundField DataField="CategoryName" HeaderText="类别" ReadOnly="True" />
11             <asp:TemplateField ShowHeader="False">
12                 <ItemTemplate>
13                     <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Bind("CategoryID") %>' OnClick="linkAssemblyDetail_OnClick"
14                         runat="server" Text="Detail"></asp:LinkButton>
15                 </ItemTemplate>
16             </asp:TemplateField>
17         </Columns>
18         <HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
19         <RowStyle HorizontalAlign="Center" />
20         <PagerStyle HorizontalAlign="Center" />
21     </asp:GridView>
22 </div>
23 

后台

代码
  1 public partial class 多层嵌套整行嵌套_ucParentGridView : System.Web.UI.UserControl
  2 {
  3     private int ShowDetailRowKey
  4     {
  5         get
  6         {
  7             if (ViewState["ShowDetailRowKey"== null)
  8             {
  9                 return -1;
 10             }
 11             else
 12             {
 13                 return int.Parse(ViewState["ShowDetailRowKey"].ToString());
 14             }
 15         }
 16         set
 17         {
 18             ViewState["ShowDetailRowKey"= value;
 19         }
 20     }
 21 
 22     protected void Page_Load(object sender, EventArgs e)
 23     {
 24         if (!IsPostBack)
 25         {
 26             bindParent();
 27         }
 28     }
 29 
 30     /// <summary>
 31     /// 数据绑定
 32     /// </summary>
 33     public void bindParent()
 34     {
 35         string sqlStr = "select * from Categories";
 36         DataSet myds = Common.dataSet(sqlStr);
 37         ParentGridView.DataSource = myds;
 38         ParentGridView.DataKeyNames = new string[] { "CategoryID" };
 39         ParentGridView.DataBind();
 40     }
 41 
 42     /// <summary>
 43     /// 在每一行绑定数据时,判断是否需要创建子表
 44     /// </summary>
 45     /// <param name="sender"></param>
 46     /// <param name="e"></param>
 47     protected void ParentGridView_RowDataBound(object sender, GridViewRowEventArgs e)
 48     {
 49         foreach (TableCell tc in e.Row.Cells)
 50         {
 51             tc.Attributes["style"= "border-color:Black";
 52         }
 53     }
 54 
 55     protected void linkAssemblyDetail_OnClick(object sender, EventArgs e)
 56     {
 57 
 58         LinkButton btn = (LinkButton)sender;
 59 
 60         if (btn.CommandArgument != string.Empty)
 61         {
 62             int key = int.Parse(btn.CommandArgument);
 63 
 64             if (key == ShowDetailRowKey)
 65             {
 66 
 67                 ShowDetailRowKey = -1;
 68 
 69                 bindParent();
 70 
 71             }
 72             else
 73             {
 74                 ShowDetailRowKey = key;
 75 
 76                 bindParent();
 77             }
 78         }
 79     }
 80 
 81     private void CreateDetailRow(GridViewRow gridRow)
 82     {
 83         if (RowIsCollasped(gridRow))
 84         {
 85             GridViewRow row = new GridViewRow(gridRow.RowIndex, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
 86 
 87             //TableCell cell = new TableCell();
 88             //row.Cells.Add(cell);
 89 
 90             TableCell cell2 = new TableCell();
 91             cell2.Attributes["colspan"= (this.ParentGridView.Columns.Count).ToString();
 92 
 93             ucChildGridView ucChildGV = (ucChildGridView)LoadControl("ucChildGridView.ascx");
 94 
 95             ucChildGV.CategoryID = int.Parse(this.ParentGridView.DataKeys[gridRow.RowIndex].Value.ToString());
 96             ucChildGV.BindGridView();
 97 
 98             cell2.Controls.Add(ucChildGV);
 99             row.Cells.Add(cell2);
100 
101 
102             this.ParentGridView.Controls[0].Controls.AddAt(gridRow.RowIndex + 2, row);
103 
104         }
105     }
106 
107     private bool RowIsCollasped(GridViewRow row)
108     {
109         return this.ShowDetailRowKey == int.Parse(this.ParentGridView.DataKeys[row.RowIndex].Value.ToString()) ? true : false;
110     }
111 
112     protected void ParentGridView_RowCreated(object sender, GridViewRowEventArgs e)
113     {
114         if (e.Row.RowType == DataControlRowType.Pager)
115         {
116             for (int i = 0; i < this.ParentGridView.Rows.Count; i++)
117             {
118                 this.CreateDetailRow(this.ParentGridView.Rows[i]);
119             }
120         }
121     }
122 }
123 

 

 

ucChildGridView.ascx 

前台

代码
 1 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucChildGridView.ascx.cs"
 2     Inherits="ucChildGridView" %>
 3 <asp:GridView ID="ChildGridView" runat="server" AllowPaging="True" PageSize="3" AutoGenerateColumns="False"
 4     BorderColor="Black" OnRowDataBound="ChildGridView_RowDataBound" Width="241px">
 5     <Columns>
 6         <asp:BoundField DataField="ProductID" HeaderText="编号" ReadOnly="True" />
 7         <asp:BoundField DataField="ProductName" HeaderText="名称" ReadOnly="True" SortExpression="ProductName" />
 8     </Columns>
 9     <HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
10     <RowStyle HorizontalAlign="Center" />
11     <PagerStyle HorizontalAlign="Center" />
12 </asp:GridView>
13  

后台 

代码
 1 public partial class ucChildGridView : System.Web.UI.UserControl
 2 {
 3     public int CategoryID
 4     {
 5         get
 6         {
 7             if (ViewState["CategoryID"== null)
 8             {
 9                 return 0;
10             }
11             else
12             {
13                 return int.Parse(ViewState["CategoryID"].ToString());
14             }
15         }
16         set
17         {
18             ViewState["CategoryID"= value;
19         }
20     }
21 
22     protected void Page_Load(object sender, EventArgs e)
23     {
24         if (!IsPostBack)
25         {
26             BindGridView();
27         }
28     }
29 
30     protected void ChildGridView_RowDataBound(object sender, GridViewRowEventArgs e)
31     {
32         foreach (TableCell tc in e.Row.Cells)
33         {
34             tc.Attributes["style"= "border-color:Black";
35         }
36 
37     }
38 
39     public void BindGridView()
40     {
41         string sqlStr = "select * from Products where CategoryID=" + this.CategoryID.ToString();
42         DataSet myds = Common.dataSet(sqlStr);
43 
44         this.ChildGridView.DataSource = myds.Tables[0];
45         this.ChildGridView.DataBind();
46     }
47 }
48 

 

 

posted @ 2010-12-15 15:28  TNTZWC  阅读(334)  评论(0编辑  收藏  举报