1 <%@ Page Language="C#" %>
2
3 <%@ Import Namespace="System.Data" %>
4
5
6
7 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
8
9
10
11 <script runat="server">
12
13 ICollection CreateDataSource()
14
15 {
16
17 DataTable dt = new DataTable();
18
19 DataRow dr;
20
21 dt.Columns.Add(new DataColumn("id", typeof(Int32)));
22
23 dt.Columns.Add(new DataColumn("text", typeof(string)));
24
25 for (int i = 0; i < 6; i++)
26
27 {
28
29 dr = dt.NewRow();
30
31 dr[0] = i;
32
33 dr[1] = "列表项目 " + i.ToString();
34
35 dt.Rows.Add(dr);
36
37 }
38
39 DataView dv = new DataView(dt);
40
41 return dv;
42
43 }
44
45
46
47 public class GridViewTemplate : ITemplate
48
49 {
50
51 private DataControlRowType templateType;
52
53 private string columnName;
54
55
56
57 public GridViewTemplate(DataControlRowType type, string colname)
58
59 {
60
61 templateType = type;
62
63 columnName = colname;
64
65 }
66
67
68
69 public void InstantiateIn(System.Web.UI.Control container)
70
71 {
72
73 switch (templateType)
74
75 {
76
77 case DataControlRowType.Header:
78
79 Literal lc = new Literal();
80
81 lc.Text = columnName;
82
83 container.Controls.Add(lc);
84
85 break;
86
87 case DataControlRowType.DataRow:
88
89 DropDownList drr = new DropDownList();
90
91 drr.ID = "dropdown";
92
93 drr.AppendDataBoundItems = true;
94
95 drr.Items.Add(new ListItem("-----请选择------", ""));
96
97 drr.Items.Add(new ListItem("AA", "a"));
98
99 drr.Items.Add(new ListItem("BB", "b"));
100
101 drr.Items.Add(new ListItem("CC", "c"));
102
103 container.Controls.Add(drr);
104
105 break;
106
107 default:
108
109 break;
110
111 }
112
113 }
114
115 }
116
117
118
119 protected void Page_Load(object sender, EventArgs e)
120
121 {
122
123 if (!IsPostBack)
124
125 {
126
127 TemplateField customField = new TemplateField();
128
129 customField.ShowHeader = true;
130
131 customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
132
133 customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
134
135 GridView1.Columns.Add(customField);
136
137 GridView1.DataSource = CreateDataSource();
138
139 GridView1.DataBind();
140
141 }
142
143 }
144
145
146
147 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
148
149 {
150
151 if (e.Row.RowType == DataControlRowType.DataRow)
152
153 {
154
155 //可以在这里访问数据库的其它字段的值,可以设置默认选择项,具体应用,看自己的发挥了。
156
157 //下面只是例子,举一反三,不再费话了
158
159 DataRowView gv = (DataRowView)e.Row.DataItem;
160
161 int itemSeleted = Int32.Parse(gv.Row["id"].ToString()) > 3 ? 0 : Int32.Parse(gv.Row["id"].ToString());
162
163 DropDownList dr = (DropDownList)e.Row.FindControl("dropdown");
164
165 dr.SelectedIndex = itemSeleted;
166
167 }
168
169 }
170
171 </script>
172
173
174
175 <html xmlns="http://www.w3.org/1999/xhtml" >
176
177 <head runat="server">
178
179 <title>GridView动态添加模板列的例子</title>
180
181 </head>
182
183 <body>
184
185 <form id="form1" runat="server">
186
187 <div>
188
189 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
190
191 OnRowDataBound="GridView1_RowDataBound">
192
193 <Columns>
194
195 <asp:BoundField HeaderText="标题" DataField="text"/>
196
197 </Columns>
198
199 </asp:GridView>
200
201 </div>
202
203 </form>
204
205 </body>
206
207 </html>
2
3 <%@ Import Namespace="System.Data" %>
4
5
6
7 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
8
9
10
11 <script runat="server">
12
13 ICollection CreateDataSource()
14
15 {
16
17 DataTable dt = new DataTable();
18
19 DataRow dr;
20
21 dt.Columns.Add(new DataColumn("id", typeof(Int32)));
22
23 dt.Columns.Add(new DataColumn("text", typeof(string)));
24
25 for (int i = 0; i < 6; i++)
26
27 {
28
29 dr = dt.NewRow();
30
31 dr[0] = i;
32
33 dr[1] = "列表项目 " + i.ToString();
34
35 dt.Rows.Add(dr);
36
37 }
38
39 DataView dv = new DataView(dt);
40
41 return dv;
42
43 }
44
45
46
47 public class GridViewTemplate : ITemplate
48
49 {
50
51 private DataControlRowType templateType;
52
53 private string columnName;
54
55
56
57 public GridViewTemplate(DataControlRowType type, string colname)
58
59 {
60
61 templateType = type;
62
63 columnName = colname;
64
65 }
66
67
68
69 public void InstantiateIn(System.Web.UI.Control container)
70
71 {
72
73 switch (templateType)
74
75 {
76
77 case DataControlRowType.Header:
78
79 Literal lc = new Literal();
80
81 lc.Text = columnName;
82
83 container.Controls.Add(lc);
84
85 break;
86
87 case DataControlRowType.DataRow:
88
89 DropDownList drr = new DropDownList();
90
91 drr.ID = "dropdown";
92
93 drr.AppendDataBoundItems = true;
94
95 drr.Items.Add(new ListItem("-----请选择------", ""));
96
97 drr.Items.Add(new ListItem("AA", "a"));
98
99 drr.Items.Add(new ListItem("BB", "b"));
100
101 drr.Items.Add(new ListItem("CC", "c"));
102
103 container.Controls.Add(drr);
104
105 break;
106
107 default:
108
109 break;
110
111 }
112
113 }
114
115 }
116
117
118
119 protected void Page_Load(object sender, EventArgs e)
120
121 {
122
123 if (!IsPostBack)
124
125 {
126
127 TemplateField customField = new TemplateField();
128
129 customField.ShowHeader = true;
130
131 customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
132
133 customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
134
135 GridView1.Columns.Add(customField);
136
137 GridView1.DataSource = CreateDataSource();
138
139 GridView1.DataBind();
140
141 }
142
143 }
144
145
146
147 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
148
149 {
150
151 if (e.Row.RowType == DataControlRowType.DataRow)
152
153 {
154
155 //可以在这里访问数据库的其它字段的值,可以设置默认选择项,具体应用,看自己的发挥了。
156
157 //下面只是例子,举一反三,不再费话了
158
159 DataRowView gv = (DataRowView)e.Row.DataItem;
160
161 int itemSeleted = Int32.Parse(gv.Row["id"].ToString()) > 3 ? 0 : Int32.Parse(gv.Row["id"].ToString());
162
163 DropDownList dr = (DropDownList)e.Row.FindControl("dropdown");
164
165 dr.SelectedIndex = itemSeleted;
166
167 }
168
169 }
170
171 </script>
172
173
174
175 <html xmlns="http://www.w3.org/1999/xhtml" >
176
177 <head runat="server">
178
179 <title>GridView动态添加模板列的例子</title>
180
181 </head>
182
183 <body>
184
185 <form id="form1" runat="server">
186
187 <div>
188
189 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
190
191 OnRowDataBound="GridView1_RowDataBound">
192
193 <Columns>
194
195 <asp:BoundField HeaderText="标题" DataField="text"/>
196
197 </Columns>
198
199 </asp:GridView>
200
201 </div>
202
203 </form>
204
205 </body>
206
207 </html>