rofans91
rofans91

Reputation: 3010

GridView ItemTemplate DropDownList Manipulation

Assume I have templated dropdown list in my gridview (which is bounded to all rows) The dropdownlist is filled through an array..

//Fill Array
private ArrayList GetDummyData()
    {
        ArrayList arr = new ArrayList();

            arr.Add(new ListItem("Item1", "1"));
            arr.Add(new ListItem("Item2", "2"));
            arr.Add(new ListItem("Item3", "3"));

        return arr;
    }

//Fill dropdownlist
private void FillDropDownList(DropDownList ddl)
    {
        ArrayList arr = GetDummyData();

        foreach (ListItem item in arr)
        {
            ddl.Items.Add(item);
        }
    }

What I want to do is asssume in gridview row[0] i selected the "Item1", so in the row[1] there is only 2 options remaining --> "Item2" and Item3"

Help is much appreciated. :)

Upvotes: 1

Views: 1220

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460168

You could handle the RowDataBound event.

For example(not tested, asssuming the DataSource is a DataTable and the ID of your DropDownList is ddl):

void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    var thisRow = (DataRowView)e.Row.DataItem;
    var source = thisRow.DataView;
    var lastRowIndex = e.Row.DataItemIndex -1;
    DataRowView lastRow = null;
    var ddl = (DropDownList)e.Item.FindControl("ddl");
    DropDownList ddlLast = null;
    if(lastRowIndex>=0){
        lastRow = source[lastRowIndex];
        ddlLast = (DropDownList)((GridView)sender).Rows[lastRowIndex].FindControl("ddl");
        //remove the items of this ddl according to the items of the last dll
    }
  }
}

You should consider that this sample won't work if you've paging enabled, because the Rows-property only returns the GridViewRows of the current page.

Edit: Maybe a better approach is to handle the DropDownList's SelectedIndexChanged event and update the itemlist of any following dropdowns:

protected void DdlSelected(object sender, EventArgs e)
{
    var ddl = (DropDownList)sender;
    var row = (GridViewRow)ddl.NamingContainer;
    var grid = (GridView)row.NamingContainer;
    var index = row.RowIndex + 1;
    while (index < grid.Rows.Count) {
        var nextRow = grid.Rows[index];
        var nextDdl = (DropDownList)nextRow.FindControl("ddl");
        nextDdl.Items.Clear();
        foreach (ListItem item in getDllSource()) {
            if (ddl.SelectedItem == null || !ddl.SelectedItem.Equals(item)) {
                nextDdl.Items.Add(item);
            }
        }
        index += 1;
    }
}

Whereas getDllSource is following function:

private List<ListItem> getDllSource()
{
    List<ListItem> items = new List<ListItem>();
    ListItem item = new ListItem("Item1", "1");
    items.Add(item);
    item = new ListItem("Item2", "2");
    items.Add(item);
    item = new ListItem("Item3", "3");
    items.Add(item);
    return items;
}

Upvotes: 2

Related Questions