0

我正在开发一个 .net 项目。我有一个gridview,并且在rowdatabound 上我想将一个下拉列表放到该行的每个单元格中。所以我有以下方法。

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DropDownList ddl = new DropDownList();
    ddl.DataSource = getImpacts();
    ddl.DataBind();
    if (e.Row.RowType != DataControlRowType.Header)
    {

        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            e.Row.Cells[i].Controls.Add(ddl);

        }
    }
}

问题是 dropdouwnlist 仅在最后一个单元格中添加!当我调试时,for循环从所有单元格传递!这怎么可能 ?

4

2 回答 2

1

需要为每一列创建一个下拉列表的实例

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.Header)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
           DropDownList ddl = new DropDownList();
           ddl.DataSource = getImpacts();
           ddl.DataBind();
           e.Row.Cells[i].Controls.Add(ddl);
        }
    }
}
于 2012-09-19T09:46:38.080 回答
1

您可以在循环中插入 for ,并为每个单元格迭代

        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
           DropDownList ddl = new DropDownList();
           ddl.DataSource = getImpacts();
           ddl.DataBind();

           e.Row.Cells[i].Controls.Add(ddl);
        }
于 2012-09-19T09:48:21.093 回答