简体   繁体   中英

How to add a DropDownList column to GridView?

How can I add a DropDownList column to gridview in code behind? In my case a want to add a dropdown called Employer . I have successfully added 1 string column called Name with the following code:

  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 .

I DON'T HAVE ACCESS TO ASPX PAGE SO I CANNOT ADD IT WITH TemplateField

Adding DropDownList dynamically to GridView in code-behind:

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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