Iamnderon
Iamnderon

Reputation: 117

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

Upvotes: 1

Views: 3208

Answers (3)

Michael Edmison
Michael Edmison

Reputation: 873

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.

Upvotes: 1

user1773603
user1773603

Reputation:

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.

Upvotes: 2

Michael Edmison
Michael Edmison

Reputation: 873

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

Upvotes: 0

Related Questions