日期:2014-05-18  浏览次数:21273 次

如何在后台移除GridView中TemplateField模板
C# code
<asp:TemplateField HeaderText="缴费方式" ItemStyle-HorizontalAlign="Center" FooterStyle-CssClass="foot">
                    <ItemTemplate>
                        <%#Eval("Pay_Type")%>
                    </ItemTemplate>

<ItemStyle HorizontalAlign="Center"></ItemStyle>
                </asp:TemplateField>


如何移除掉TemplateField???

------解决方案--------------------
可以这样啊
C# code
for (int i = 0; i < GridView1.Columns.Count; i++)
{
  if (GridView1.Columns[i].HeaderText.Equals("缴费方式"))
  {
    GridView1.Columns.RemoveAt(1);
  }
}
GridView1.DataSource = dataTable1;
GridView1.DataBind();

------解决方案--------------------
完整的测试例子
HTML code
<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
    System.Data.DataRow dr;
    dataTable1.Columns.Add(new System.Data.DataColumn("UserId", typeof(System.Int32)));
    dataTable1.Columns.Add(new System.Data.DataColumn("UserName", typeof(System.String)));
    dataTable1.PrimaryKey = new System.Data.DataColumn[] { dataTable1.Columns["UserId"] };

    for (int i = 0; i < 8; i++)
    {
      dr = dataTable1.NewRow();
      dr[0] = i;
      dr[1] = "【孟子E章】" + i.ToString();
      dataTable1.Rows.Add(dr);
    }
    for (int i = 0; i < GridView1.Columns.Count; i++)
    {
      if (GridView1.Columns[i].HeaderText.Equals("缴费方式"))
      {
        GridView1.Columns.RemoveAt(1);
      }
    }
    GridView1.DataSource = dataTable1;
    GridView1.DataBind();
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
      <asp:BoundField DataField="UserId" />
      <asp:TemplateField HeaderText="缴费方式" ItemStyle-HorizontalAlign="Center" FooterStyle-CssClass="foot">
        <ItemTemplate>
          <%#Eval("UserName")%>
        </ItemTemplate>
      </asp:TemplateField>
    </Columns>
  </asp:GridView>
  </form>
</body>
</html>