【发布时间】:2011-08-26 10:28:45
【问题描述】:
我正在尝试在 ASP.NET 2.0 中向 GridView 添加一列
gridViewPoco.Columns.Add(...)
但是,我找不到正确的选项。我想要以下等价物:
<asp:BoundField>
<asp:TemplateField>
【问题讨论】:
标签: c# asp.net gridview code-behind
我正在尝试在 ASP.NET 2.0 中向 GridView 添加一列
gridViewPoco.Columns.Add(...)
但是,我找不到正确的选项。我想要以下等价物:
<asp:BoundField>
<asp:TemplateField>
【问题讨论】:
标签: c# asp.net gridview code-behind
例如;
protected void Btn_AddCol_Click(object sender, EventArgs e)
{
TemplateField tf = new TemplateField();
tf.HeaderTemplate = new GridViewLabelTemplate(DataControlRowType.Header, "Col1", "Int32");
tf.ItemTemplate = new GridViewLabelTemplate(DataControlRowType.DataRow, "Col1", "Int32");
MyGridView.Columns.Add(tf);
}
TemplateField
Col1)和类型(Int32)Int32)Gridview
【讨论】:
Soner's Answer 非常适合将列添加到 Gridview 的末尾。但是,如果您发现自己需要在 GridView 的中间添加列,则需要采用稍微不同的路径(使用 MyGridView.Columns.Insert() 函数):
protected void Btn_AddCol_Click(object sender, EventArgs e)
{
TemplateField tf = new TemplateField();
tf.HeaderTemplate = new GridViewLabelTemplate(DataControlRowType.Header, "Col1", "Int32");
tf.ItemTemplate = new GridViewLabelTemplate(DataControlRowType.DataRow, "Col1", "Int32");
MyGridView.Columns.Insert(2, tf); //the 2 makes it go into the third column -- zero-based indexing ftw
}
【讨论】: