简体   繁体   English

如何将DropDownList列添加到GridView?

[英]How to add a DropDownList column to GridView?

How can I add a DropDownList column to gridview in code behind? 如何在后面的代码中将DropDownList列添加到gridview? In my case a want to add a dropdown called Employer . 在我的情况下,想要添加一个名为Employer的下拉列表。 I have successfully added 1 string column called Name with the following code: 我已经使用以下代码成功添加了一个名为Name字符串列:

  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);

The Name column is working wonderful but the Employer is showing this message in each row System.Web.UI.WebControls.DropDownList . Name列的工作很棒,但是Employer在每行System.Web.UI.WebControls.DropDownList显示此消息。

I DON'T HAVE ACCESS TO ASPX PAGE SO I CANNOT ADD IT WITH TemplateField 我无法访问ASPX页面,因此无法使用TemplateField添加它

Adding DropDownList dynamically to GridView in code-behind: 在后台代码中将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
    }
}

Now there are created DropDownLists to all GridView rows and you can bind it in RowDataBound event of GridView. 现在,已为所有GridView行创建了DropDownLists,您可以将其绑定到GridView的RowDataBound事件中。

to follow your previous code starting this post, here is an updated version you can follow that should get it for you. 为了遵循本文开头的先前代码,您可以按照以下更新版本进行操作,该版本应该适合您。 (again, following your syntax) (再次,遵循您的语法)

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);
    }

You can adjust how ever, as I may have mispoken above walking through this scenario. 您可以进行调整,因为我可能在讲解这种情况时已经犯了一个错误。

you need to look at the data/values in it. 您需要查看其中的数据/值。 (WRONG statement on my end) (对我的说法有误)

Here it shows you need to add the control, and the controls shows the data/values in it. 在这里它显示您需要添加控件,并且控件在其中显示数据/值。 (had this scenario happen in winforms and this is a bit different). (如果这种情况在winforms中发生,则有些不同)。

Hope that helps. 希望能有所帮助。

The reason your seeing the "system.web..." in the Employer is due to the DropDownList is the object, and you need to look at the data/values in it. 您在雇主中看到“ system.web ...”的原因是由于DropDownList是对象,因此您需要查看其中的数据/值。

adding some definition to that column should do the trick. 向该列添加一些定义应该可以解决问题。

Heres a previous working example to look at that column by itself. 这是一个以前的工作示例,可以单独查看该列。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM