ASP.NET 2.0 中动态添加 GridView 模板列的例子
2007-05-04 15:26
453 查看
动态添加列,关键是实现 ITemplate.InstantiateIn 方法。下面是一个添加 GridView 模板列的例子。
C#代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ Page Language="C#" %>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ Import Namespace="System.Data" %>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script runat="server">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ICollection CreateDataSource()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataTable dt = new DataTable();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataRow dr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Columns.Add(new DataColumn("id", typeof(Int32)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Columns.Add(new DataColumn("text", typeof(string)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 6; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr = dt.NewRow();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr[0] = i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr[1] = "列表项目 " + i.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Rows.Add(dr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataView dv = new DataView(dt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return dv;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class GridViewTemplate : ITemplate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private DataControlRowType templateType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private string columnName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public GridViewTemplate( DataControlRowType type, string colname )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
templateType = type;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
columnName = colname;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void InstantiateIn( System.Web.UI.Control container )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
switch (templateType)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case DataControlRowType.Header:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Literal lc = new Literal();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lc.Text = columnName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
container.Controls.Add(lc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case DataControlRowType.DataRow:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DropDownList drr = new DropDownList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.ID = "dropdown";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.AppendDataBoundItems = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.Items.Add(new ListItem("-----请选择------",""));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.Items.Add(new ListItem("AA", "a"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.Items.Add(new ListItem("BB", "b"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.Items.Add(new ListItem("CC", "c"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
container.Controls.Add(drr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
default:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!IsPostBack)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TemplateField customField = new TemplateField();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
customField.ShowHeader = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.Columns.Add(customField);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.DataSource = CreateDataSource();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.DataBind();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Row.RowType == DataControlRowType.DataRow)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//可以在这里访问数据库的其它字段的值,可以设置默认选择项,具体应用,看自己的发挥了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//下面只是例子,举一反三,不再费话了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataRowView gv = (DataRowView)e.Row.DataItem;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int itemSeleted = Int32.Parse(gv.Row["id"].ToString()) > 3 ? 0 : Int32.Parse(gv.Row["id"].ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DropDownList dr = (DropDownList)e.Row.FindControl("dropdown");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr.SelectedIndex = itemSeleted;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head id="Head1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>GridView动态添加模板列的例子</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
OnRowDataBound="GridView1_RowDataBound">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<Columns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:BoundField HeaderText="标题" DataField="text"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</Columns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</asp:GridView>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
C#代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ Page Language="C#" %>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<%...@ Import Namespace="System.Data" %>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script runat="server">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ICollection CreateDataSource()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataTable dt = new DataTable();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataRow dr;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Columns.Add(new DataColumn("id", typeof(Int32)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Columns.Add(new DataColumn("text", typeof(string)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < 6; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr = dt.NewRow();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr[0] = i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr[1] = "列表项目 " + i.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dt.Rows.Add(dr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataView dv = new DataView(dt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return dv;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class GridViewTemplate : ITemplate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private DataControlRowType templateType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private string columnName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public GridViewTemplate( DataControlRowType type, string colname )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
templateType = type;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
columnName = colname;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void InstantiateIn( System.Web.UI.Control container )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
switch (templateType)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case DataControlRowType.Header:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Literal lc = new Literal();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lc.Text = columnName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
container.Controls.Add(lc);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case DataControlRowType.DataRow:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DropDownList drr = new DropDownList();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.ID = "dropdown";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.AppendDataBoundItems = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.Items.Add(new ListItem("-----请选择------",""));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.Items.Add(new ListItem("AA", "a"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.Items.Add(new ListItem("BB", "b"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
drr.Items.Add(new ListItem("CC", "c"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
container.Controls.Add(drr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
default:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!IsPostBack)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TemplateField customField = new TemplateField();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
customField.ShowHeader = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.Columns.Add(customField);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.DataSource = CreateDataSource();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GridView1.DataBind();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (e.Row.RowType == DataControlRowType.DataRow)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//可以在这里访问数据库的其它字段的值,可以设置默认选择项,具体应用,看自己的发挥了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//下面只是例子,举一反三,不再费话了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataRowView gv = (DataRowView)e.Row.DataItem;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int itemSeleted = Int32.Parse(gv.Row["id"].ToString()) > 3 ? 0 : Int32.Parse(gv.Row["id"].ToString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DropDownList dr = (DropDownList)e.Row.FindControl("dropdown");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dr.SelectedIndex = itemSeleted;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head id="Head1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>GridView动态添加模板列的例子</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
OnRowDataBound="GridView1_RowDataBound">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<Columns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:BoundField HeaderText="标题" DataField="text"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</Columns>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</asp:GridView>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- [导入]ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET 2.0中动态添加 GridView 模板列的例子
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- ASP.NET 2.0 中动态添加 GridView 模板列
- ASP.NET 2.0中动态添加GridView
- asp.net动态添加GridView的模板列,并获取列值
- asp.Net中Gridview动态创建模板列