user7415073
user7415073

Reputation: 300

Remove item from dropdown permanently inside the Gridview

I am having the dropdownList inside the Gridview like,

<asp:TemplateField HeaderText="Leave Category" >
    <ItemTemplate>
        <asp:DropDownList ID="LCList" runat ="server"  AutoPostBack="true" OnSelectedIndexChanged="LCList_TextChanged"/>
    </ItemTemplate>
</asp:TemplateField>

and adding items like,

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList LCList = (e.Row.FindControl("LCList") as DropDownList);
        LCList.Items.Insert(0, new ListItem("casual Leave"));
        LCList.Items.Insert(1, new ListItem("sick Leave"));
        LCList.Items.Insert(2, new ListItem("LOP"));
    }
}

if I choose LOP once, it should be removed from the selected row dropdown list of a grid. It won't be show again.

The above code is working. But if I refresh the page, it is displaying

How to solve this?

Is it need to create the items in any other event?

Upvotes: 3

Views: 1164

Answers (2)

VDWWD
VDWWD

Reputation: 35514

It is displaying because you will have to store the removed value(s) from the DropDownList somewhere, like a Session. You will have to save the row number and removed value, and then when building the GridView compare the values in the DropDown against the ones that have already been removed.

//create a list with keyvalue pairs to hold the removed items
public static List<KeyValuePair<int, string>> itemsRemoved = new List<KeyValuePair<int, string>>();

protected void Page_Load(object sender, EventArgs e)
{
    //check if the session with the removed items exists and if so cast back to a list
    if (Session["itemsRemoved"] != null)
    {
        itemsRemoved = Session["itemsRemoved"] as List<KeyValuePair<int, string>>;
    }

    //bind the gridview
    if (!Page.IsPostBack)
    {
        GridView1.DataSource = Common.LoadFromDB();
        GridView1.DataBind();
    }
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a normal datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList LCList = (e.Row.FindControl("LCList") as DropDownList);

        //create an array with the dropdown option for easier looping
        string[] listItems = new string[] { "casual Leave", "sick Leave", "LOP" };

        for (int i = 0; i < listItems.Length; i++)
        {
            bool wasRemoved = false;

            //check if the listitem was remove for this row
            for (int j = 0; j < itemsRemoved.Count; j++)
            {
                if (e.Row.RowIndex == itemsRemoved[j].Key && listItems[i] == itemsRemoved[j].Value)
                {
                    wasRemoved = true;
                }
            }

            //if not removed, add it to the dropdownlist
            if (wasRemoved == false)
            {
                LCList.Items.Insert(LCList.Items.Count, new ListItem(listItems[i]));
            }
        }
    }
}

protected void LCList_SelectedIndexChanged(object sender, EventArgs e)
{
    //cast the sender back to a dropdownlist
    DropDownList LCList = sender as DropDownList;

    //get tne namingcontainer from the dropdownlist to get the rownumber
    GridViewRow row = (GridViewRow)LCList.NamingContainer;
    int rowIndex = row.RowIndex;

    //create a new keyvalue pair with the correct rowindex and selected value
    KeyValuePair<int, string> kv = new KeyValuePair<int, string>(rowIndex, LCList.SelectedValue);

    //add it to the list with removals
    itemsRemoved.Add(kv);

    //remove from the dropdownlist immediately
    LCList.Items.RemoveAt(LCList.SelectedIndex);
}

Upvotes: 1

Sailor
Sailor

Reputation: 183

In post back (when you refresh your page) the DataBound method is being executed again and that is why you are seeing all values. You have to tweak your databound method to check the value before binding Data to grid.

just before your Databind call ...

// Check if any of the Grid value has LOP selected

if (DataTable.Select(dr => dr.dropDown.Value = "LOP").Count > 0) 
{
 blnIsLOPselected = True;
}

DataGrid.DataSource = <dataTable>
DataGrid.DataBind();

And in your DataBind

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList LCList = (e.Row.FindControl("LCList") as DropDownList);
        LCList.Items.Insert(0, new ListItem("casual Leave"));
        LCList.Items.Insert(1, new ListItem("sick Leave"));

        If (!blnIsLOPselected )
        {
           LCList.Items.Insert(2, new ListItem("LOP"));
        }
    }
}

Upvotes: 3

Related Questions