2010-09-07 63 views

回答

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Text; 

public partial class GridViewHeaderDDL : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Person[] people = new List<Person>() 
     { 
      new Person() {ID = 1, name = "John" }, 
      new Person() {ID = 2, name = "Dave" } 
     }.ToArray(); 

     GridView gvw = new GridView(); 
     gvw.RowCreated += new GridViewRowEventHandler(gvw_RowCreated); 
     gvw.DataSource = people; 
     gvw.DataBind(); 
     this.form1.Controls.Add(gvw); 
    } 

    void gvw_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      foreach (TableCell itm in e.Row.Cells) 
      { 
       itm.Text += CreateDDL(); 
      } 
     } 
    } 

    private string CreateDDL() 
    { 
     StringBuilder sb = new StringBuilder(); 
     sb.Append("<select>"); 
     sb.Append(@"<option value=""1"">One</option>"); 
     sb.Append(@"<option value=""2"">Two</option>"); 
     sb.Append("</select>"); 

     return sb.ToString(); 
    } 

    public class Person 
    { 
     public int ID {get; set;} 
     public string name { get; set; } 
    } 
} 

嘗試在本地運行此測試頁並從此處進行修改。

的代碼做什麼:

  1. 負載啞數據。
  2. 創建GridView。
  3. 綁定gridview與虛擬數據並添加到表單控件。
  4. 在RowCreated事件上,它會在標題單元格中加載一個下拉列表。

這是屏幕截圖:

alt text

相關問題