user1292656
user1292656

Reputation: 2560

Gridview row with dropdownlist

I am working on an asp .net project. I have a gridview and on rowdatabound i want to put a dropdownlist to every cell of the row. 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! and when i debug, the for loop passes from all the cells!. How is this possible ?

Upvotes: 0

Views: 603

Answers (2)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

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

Upvotes: 1

Prasanna
Prasanna

Reputation: 4703

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

Upvotes: 1

Related Questions