Search code examples
c#asp.netwebformsdropdownbox

Deleting items from a DropDownList in ASP.NET C#


I have a DropDownList in my ASPX page:

<asp:DropDownList ID="ddlGroups" runat="server" Width="200px" AppendDataBoundItems="true"
            AutoPostBack="true" OnSelectedIndexChanged="ddlGroups_SelectionIndexChanged">
            <asp:ListItem Text="All" Value="0"></asp:ListItem>
            <asp:ListItem Text="Pending" Value="1"></asp:ListItem>
            <asp:ListItem Text="Completed" Value="2"></asp:ListItem>
</asp:DropDownList>

Also based on a condition I am populating the DropDownList in my code behind :

  List<Status> lstStatus  = new List<Status>();
  lstStatus = GetStatus() //Get's the list from db
  if (lstStatus != null && lstStatus.Count > 0)
    {
        ddlGroups.DataSource = lstStatus;
        ddlGroups.DataTextField = "StatusDescription";
        ddlGroups.DataValueField = "StatusID";
        ddlGroups.DataBind();
       /* ddlGroups.
        ddlGroups.Items.Remove("Completed");*/
    }

When I do this, I do not want the DropDownList to be poulated from the ASP.NET page markup. I.e. I want to remove the items added from the aspx code (only two of them).

Any help?


Solution

  • Clear Dropdown and fill it as you want.

    DropDownList1.Items.Clear();