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