【问题标题】:How to add a DropDownList column to GridView?如何将 DropDownList 列添加到 GridView?
【发布时间】:2017-08-16 13:36:27
【问题描述】:

如何在后面的代码中将DropDownList 列添加到gridview?在我的例子中,我想添加一个名为Employer 的下拉列表。我已经成功添加了 1 个名为 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);

Name 列运行良好,但Employer 在每一行都显示此消息System.Web.UI.WebControls.DropDownList

我无权访问 ASPX 页面,因此无法使用 TemplateField 添加它

【问题讨论】:

  • 您不能将 DropdownList 对象绑定到 DataRow,您必须从 DropdownsList 中选择一个属性才能显示。例如:drp.Employer.Name

标签: c# asp.net gridview


【解决方案1】:

在代码隐藏中将 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
    }
}

现在已经为所有 GridView 行创建了 DropDownLists,您可以将其绑定到 GridView 的RowDataBound 事件中。

【讨论】:

  • 但我需要重新排序下拉解决方案,因为它显示为第一列
  • 在您的上述问题中,您尝试在数据表而不是 gridview 中动态创建下拉列表。你能说得更具体点吗?
【解决方案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);
    }

您可以调整方式,因为我可能在上面介绍这种情况时说错了。

您需要查看其中的数据/值。 (我的说法是错误的)

这里显示需要添加控件,控件显示其中的数据/值。 (如果这种情况发生在 winforms 中,这有点不同)。

希望对您有所帮助。

【讨论】:

    【解决方案3】:

    您在 Employer 中看到“system.web...”的原因是因为 DropDownList 是对象,您需要查看其中的数据/值。

    向该列添加一些定义应该可以解决问题。

    这是一个单独查看该列的先前工作示例。

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

    【讨论】:

    • 我还是解决不了。您能否更具体地说明我应该怎么做?
    • 如果下面的注释没有提供足够的例子,那么我可以得到一个可行的解决方案。
    • @lamnderon - 我在上面发布的那个例子有没有给你想要的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 2011-03-05
    • 1970-01-01
    • 2011-01-17
    • 2016-12-10
    • 1970-01-01
    相关资源
    最近更新 更多