2017-08-16 47 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中,这有点不同)。

希望有所帮助。