简体   繁体   English

Gridview行与下拉列表

[英]Gridview row with dropdownlist

I am working on an asp .net project. 我正在开发一个asp .net项目。 I have a gridview and on rowdatabound i want to put a dropdownlist to every cell of the row. 我有一个gridview,在rowdatabound上我想把一个下拉列表放到行的每个单元格中。 So i have the following method. 所以我有以下方法。

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

        }
    }
}

The problem is that the dropdouwnlist is added only at the last cell! 问题是dropdouwnlist只在最后一个单元格中添加! and when i debug, the for loop passes from all the cells!. 当我调试时,for循环从所有单元格传递! How is this possible ? 这怎么可能 ?

Need to create an instance of drop down list for each column 需要为每列创建下拉列表的实例

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

You can insert in your loop for , and iterate for each cell 您可以在循环中插入,并为每个单元格迭代

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

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

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