1
CS:
2
using System;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Data;
6
using System.Drawing;
7
using System.Web;
8
using System.Web.SessionState;
9
using System.Web.UI;
10
using System.Web.UI.WebControls;
11
using System.Web.UI.HtmlControls;
12
using System.Data.SqlClient;
13
namespace Demo
14
{
15
/// <summary>
16
/// WebForm9 的摘要说明。
17
/// </summary>
18
public class WebForm9 : System.Web.UI.Page
19
{
20
protected System.Web.UI.WebControls.DataGrid dgParent;
21
22
23
private void Page_Load(object sender, System.EventArgs e)
24
{
25
//DataGrid 中嵌套 DataGrid 的做法及方法
26
27
if(!Page.IsPostBack)
28
{
29
DataGridBind();
30
}
31
}
32
private void DataGridBind()
33
{
34
35
//使用datagrid 显示层次数据
36
37
//DataRowView view=new DataRowView();
38
39
//view.CreateChildView()
40
41
//中的参数是:
42
43
//System.Data.DataRelation 的名称的字符串
44
45
//datasource='<%# ((DataRowView)Container.DataItem).CreateChildView("tableRelation") %>'
46
47
//这句话的解释是:
48
49
//具有指定的 System.Data.DataRelation 名称的子级System.Data.DataTable 的System.Data.DataView 的一个视图
50
51
52
53
SqlConnection conn = new SqlConnection("server=.;user=sa;pwd=;database=pubs");
54
55
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM authors; SELECT * FROM titleauthor",conn);
56
57
DataSet ds = new DataSet();
58
59
try
60
{
61
//填充数据集
62
da.Fill(ds);
63
64
//设定表名字
65
ds.Tables[0].TableName = "authors";
66
67
ds.Tables[1].TableName = "titleauthor";
68
69
DataColumn Parent = ds.Tables["authors"].Columns["au_id"];//父表id的集合
70
71
DataColumn Child = ds.Tables["titleauthor"].Columns["au_id"]; //子表id的集合
72
73
// 父主表的id必须是关联的
74
75
DataRelation tableRelation = new DataRelation("tableRelation", Parent, Child, false);//表示两个table之间的关系,false 是不设置约束
76
77
ds.Relations.Add(tableRelation);
78
79
//进行数据绑定
80
dgParent.DataSource = ds.Tables["authors"].DefaultView;
81
82
dgParent.DataBind();
83
84
}
85
catch(System.Data.SqlClient.SqlException e)
86
{
87
throw new Exception(e.Message);
88
89
}
90
finally
91
{
92
conn.Close();
93
conn.Dispose();
94
da.Dispose();
95
ds.Dispose();
96
}
97
98
}
99
100
Web 窗体设计器生成的代码
120
}
121
}
122
123
124
HTML:
125
<%@ Page language="c#" Codebehind="WebForm9.aspx.cs" AutoEventWireup="false" Inherits="Demo.WebForm9" %>
126
<%@ Import NameSpace="System.Data"%>
127
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
128
<html>
129
<head>
130
<title></title>
131
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
132
<meta content="C#" name="CODE_LANGUAGE">
133
<meta content="JavaScript" name="vs_defaultClientScript">
134
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
135
</head>
136
<body>
137
<form id="form1" method="post" runat="server">
138
<asp:datagrid id="dgParent" runat="server" autogeneratecolumns="False" cellpadding="4">
139
<columns>
140
<asp:templatecolumn>
141
<itemtemplate>
142
<table cellspacing="0" cellpadding="0" width="100%" border="0">
143
<tr>
144
<td bgcolor="#3399ff"><b>根目录:ID
145
<%# DataBinder.Eval(Container.DataItem, "au_id") %>
146
</b>
147
<td bgcolor="#3399ff">
148
名称:
149
<%# ((DataRowView)Container.DataItem)["au_lname"] %>
150
</td>
151
</td>
152
</tr>
153
<tr>
154
<td>
155
<asp:datagrid id="dgChild" runat="server" autogeneratecolumns="False" datasource='<%# ((DataRowView)Container.DataItem).CreateChildView("tableRelation") %>' datakeyfield="au_id" width="100%" >
156
<columns>
157
<asp:boundcolumn datafield="au_id" headertext="ID"></asp:boundcolumn>
158
<asp:boundcolumn datafield="title_id" headertext="子目录"></asp:boundcolumn>
159
</columns>
160
</asp:datagrid></td>
161
</tr>
162
</table>
163
</itemtemplate>
164
</asp:templatecolumn>
165
</columns>
166
<pagerstyle horizontalalign="Left" forecolor="#003399" backcolor="#99CCCC" mode="NumericPages"></pagerstyle>
167
</asp:datagrid></form>
168
</body>
169
</html>
170
171
172

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172
