ASP.NET 2.0 中动态添加 GridView 模板列的...

本文介绍了如何在ASP.NET 2.0中实现GridView控件的模板列动态添加,包括创建数据源、定义模板字段及其实现类,并通过示例代码展示了具体的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
framespacing="0" src="http://what.yesky.com/by/ztdh.htm" frameborder="0" width="1003" scrolling="no" height="60">
  登陆 | 注册

进入主题社区 按内容查找 按标题查找
marginwidth="0" marginheight="0" src="/commiframe/commendtags.html" frameborder="0" noresize="noresize" width="280" scrolling="no" height="50">
 
你的位置: 天极主题社区 程序开发.NETASP.NET源码
1楼  发表:ASP.NET 2.0 中动态添加 GridView 模板列的例子
独行客2006-09-21 15:50 赞成 反对  回复 报警
<%...@ Page Language="C#" %>
<%...@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>

<script runat="server">...
 ICollection CreateDataSource()
 ...{
   DataTable dt = new DataTable();
   DataRow dr;
   dt.Columns.Add(new DataColumn("id", typeof(Int32)));
   dt.Columns.Add(new DataColumn("text", typeof(string)));
   for (int i = 0; i < 6; i++)
   ...{
     dr = dt.NewRow();
     dr[0] = i;
     dr[1] = "列表项目 " + i.ToString();
     dt.Rows.Add(dr);
   }
   DataView dv = new DataView(dt);
   return dv;
 }

 public class GridViewTemplate : ITemplate
 ...{
   private DataControlRowType templateType;
   private string columnName;

   public GridViewTemplate( DataControlRowType type, string colname )
   ...{
     templateType = type;
     columnName = colname;
   }

   public void InstantiateIn( System.Web.UI.Control container )
   ...{
     switch (templateType)
     ...{
       case DataControlRowType.Header:
         Literal lc = new Literal();
         lc.Text = columnName;          
         container.Controls.Add(lc);          
         break;
       case DataControlRowType.DataRow:
         DropDownList drr = new DropDownList();
         drr.ID = "dropdown";
         drr.AppendDataBoundItems = true;
         drr.Items.Add(new ListItem("-----请选择------",""));
         drr.Items.Add(new ListItem("AA", "a"));
         drr.Items.Add(new ListItem("BB", "b"));
         drr.Items.Add(new ListItem("CC", "c"));
         container.Controls.Add(drr);
         break;
       default:
        break;
     }
   }
 }
 
 protected void Page_Load(object sender, EventArgs e)
 ...{
   if (!IsPostBack)
   ...{
     TemplateField customField = new TemplateField();
     customField.ShowHeader = true;
     customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
     customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "");
     GridView1.Columns.Add(customField);
     GridView1.DataSource = CreateDataSource();
     GridView1.DataBind();
   }
 }

 protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )
 ...{
   if (e.Row.RowType == DataControlRowType.DataRow)
   ...{
     //可以在这里访问数据库的其它字段的值,可以设置默认选择项,具体应用,看自己的发挥了。
     //下面只是例子,举一反三,不再费话了
     DataRowView gv = (DataRowView)e.Row.DataItem;
     int itemSeleted = Int32.Parse(gv.Row["id"].ToString()) > 3 ? 0 : Int32.Parse(gv.Row["id"].ToString());
     DropDownList dr = (DropDownList)e.Row.FindControl("dropdown");
     dr.SelectedIndex = itemSeleted;
   }
 }
</script>

<html xmlns=" http://www.w3.org/1999/xhtml";>
<head id="Head1" runat="server">
 <title>GridView动态添加模板列的例子</title>
</head>
<body>
<form id="form1" runat="server">
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    OnRowDataBound="GridView1_RowDataBound">
   <Columns>
     <asp:BoundField HeaderText="标题"  DataField="text"/>
   </Columns>
 </asp:GridView>
</form>
</body>
</html>
本主题共有回贴0条。 共有[ 0]条记录,0/0页。 返回主题列表
id="rtf" style="WIDTH: 0px; HEIGHT: 0px" marginwidth="0" marginheight="0" src="about:blank" scrolling="no">
发表评论:(Html:禁止,UBB:允许)
笔名: 登录   ( 可输入2000 个字 )
src="/review.upload.do" frameborder="0" width="100%" scrolling="no" height="25">
  粗体 图片 Flash图片 Media Player视频文件 realplay视频文件 超级连接 Email连接 Shockwave文件 引用文字
 

pic
Copyright (C) 1999-2005 Chinabyte.com, All Rights Reserved 版权所有 天极网络

渝ICP证B2-20030003号 如有意见请与我们联系 Powered by 天极内容管理平台CMS4i
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值