2017-08-16 48 views
2

如何在代碼後面的gridview中添加DropDownList列?在我的情況下,想要添加名爲Employer的下拉列表。我已經成功地將1個名爲Name用下面的代碼串柱:如何向GridView添加DropDownList列?

DataTable dt = new DataTable(); 
    DropDownList drp = new DropDownList(); 

    dt.Columns.Add("Name", typeof(string)); 
    dt.Columns.Add("Employer", typeof(DropDownList)); 

    drp.Items.Add(new ListItem("test", "0")); 

    foreach (SPListItem item in queryResults) 
    { 

     dr["Name"] = item["iv3h"].ToString(); 
     dr["Employer"] = drp; 
     dt.Rows.Add(dr); 
    } 

    BoundField bf = new BoundField(); 
    bf.DataField = "Name"; 
    bf.HeaderText = "Name"; 
    bf.HeaderStyle.HorizontalAlign = HorizontalAlign.Left; 
    bf.ItemStyle.HorizontalAlign = HorizontalAlign.Left; 
    GridViewEarningLine.Columns.Add(bf); 

Name列正在精彩,但Employer是顯示各行System.Web.UI.WebControls.DropDownList在此消息。

我沒有訪問ASPX頁面,讓我無法添加它TemplateField

+0

您無法將DropdownList對象綁定到DataRow中,您必須從DropdownsList中選擇一個屬性進行顯示。 例如:drp.Employer.Name – ShaiEitan

回答

2

添加的DropDownList動態地GridView控件代碼隱藏:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.Row) 
    { 
     DropDownList ddl = new DropDownList(); 

     ddl.AutoPostBack = true; 

     // add index changed events to dropdownlists 
     ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged); 

     e.Row.Cells[1].Controls.Add(ddl); // add dropdownlist to column two 
    } 
} 

現在有創建DropDownLists所有的GridView行,您可以在RowDataBound事件的GridView的綁定。

+0

但我需要重新排列下拉解決方案,因爲它顯示爲第一列 – Iamnderon

+0

在上面的問題中,您試圖在數據表中動態創建Dropdownlist而不是gridview。你能更具體嗎? – AsifAli72090

0

你看到的原因「的System.Web ......」在僱主是由於DropDownList爲對象,您需要查看其中的數據/值。

添加一些定義到該列應該做的伎倆。

繼承人以前的工作示例單獨查看該列。

https://stackoverflow.com/a/14105600/2484080

+0

我仍然無法解決它。你能更具體地說我該怎麼做? – Iamnderon

+0

如果下面的註釋沒有提供足夠的例子,那麼我可以得到一個工作解決方案。 –

+0

好的,你可以展示它嗎? –

2

要按照您之前的代碼開始這篇文章,這裏是一個更新的版本,你可以按照應該得到它爲你。 (再次,按照你的語法)

DataTable dt = new DataTable(); 
    DropDownList drp = new DropDownList(); 

    dt.Columns.Add("Name", typeof(string)); 
    dt.Columns.Add("Employer", typeof(DropDownList)); 

    drp.Items.Add(new ListItem("test", "0")); 

    foreach (SPListItem item in queryResults) 
    { 

     dr["Name"] = item["iv3h"].ToString(); 
     //dr["Employer"] = drp; --object being added when you need the control. 
     dt.Rows.Add(dr); 
    } 

    BoundField bf = new BoundField(); 
    bf.DataField = "Name"; 
    bf.HeaderText = "Name"; 
    bf.HeaderStyle.HorizontalAlign = HorizontalAlign.Left; 
    bf.ItemStyle.HorizontalAlign = HorizontalAlign.Left; 
    GridViewEarningLine.Columns.Add(bf); 

//Here is how you can add the control with the contents as needed. 
    foreach (GridViewRow row in GridViewEarningLine.Rows) 
    { 
     drp = new DropDownList(); 
     drp.DataSource = list; 
     drp.DataBind(); 
     drp.SelectedIndex = -1; 
     row.Cells[1].Controls.Add(drp); 
    } 

您可以調整怎麼過,因爲我可能已經通過上面這個場景走mispoken。

您需要查看其中的數據/值。 (在我的結尾的錯誤聲明)

這裏顯示您需要添加控件,並且控件顯示其中的數據/值。 (這種情況發生在winforms中,這有點不同)。

希望有所幫助。