惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - Flymouse

Error CS0006 when invoking MSBuild from command line 一个Cookie引起的混乱 数据库错误:在执行批处理时出现错误。错误消息为: 目录名无效 NorthScale MemBase Server 1.6 的使用 使用网络目录映射虚拟目录出现 500.19 权限不足问题的一个解决办法 URL重写给 asp.net Ajax带来的问题 调整viewState的位置,有助于SEO C# Excel进程关闭 - Flymouse - 博客园 关于限制水晶报表的导出格式 2个Excel异常处理 Win7 x64 IIS7支持32位应用程序 在.net中与mysql数据库的时候碰到显示不了中文字符的情况实现! 完美解决IFRAME中COOKIE、SESSION丢失 关于Ucenter在IIS中有时出现“Access denied for agent changed”错误。 如何將 Access 的 Memo 型態欄位匯入到 SQL2005 的 nvarchar 型態欄位 .net wap强制输出WML - Flymouse - 博客园 使用Extjs的Form无法输入的问题 jQuery滚屏代码,还有一点地方封装不进去 - Flymouse - 博客园 关于使用 jquery Validate 使用出现的问题
ASP.NET 2.0 中动态添加 GridView 模板列的例子
Flymouse · 2007-11-27 · via 博客园 - Flymouse

动态添加列,关键是实现 ITemplate.InstantiateIn 方法。下面是一个添加 GridView 模板列的例子。

<%@ 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>