title: Show items in dropdownlist that is not equal to the selected item in other dropdownlist
body: <p>Example I have 5 <code>DropDownList</code> with values</p>
  
  <pre><code>red, orange, yellow, green, blue
  </code></pre>
  
  <p>if <code>DropDownList1</code> select red</p>
  
  <p>The choices for <code>DropDownList2-5</code> will be</p>
  
  <pre><code>orange, yellow, green, blue
  </code></pre>
  
  <p>if <code>DropDownList2</code> select <code>yellow</code></p>
  
  <p>The choices for <code>DropDownList3-5</code> will be</p>
  
  <pre><code>orange, green, blue
  </code></pre>
  
  <hr>
  
  <p>Here is the code I found in the internet, this is only for 3 <code>DropDownList</code> but what I need is for 5 <code>DropDownList</code>. I can't expand the code for 5 <code>DropDownList</code></p>
  
  <p><strong>VB</strong></p>
  
  <p>Imports System.Data
  Imports System.Data.SqlClient</p>
  
  <p>Partial Class _Default</p>
  
  <pre><code>Inherits System.Web.UI.Page
  Private bFlag As Boolean = True
  
  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
      If Not IsPostBack Then
          FillddlLocations()
      End If
  End Sub
  
  'Properties to store selected value in ViewState
  
  Protected Property MemberID1Selection() As String
      Get
          If ViewState("MemberID1Selection") IsNot Nothing Then
              Return ViewState("MemberID1Selection").ToString()
          End If
          Return ""
      End Get
      Set(value As String)
          ViewState("MemberID1Selection") = value
      End Set
  End Property
  
  Protected Property MemberID2Selection() As String
      Get
          If ViewState("MemberID2Selection") IsNot Nothing Then
              Return ViewState("MemberID2Selection").ToString()
          End If
          Return ""
      End Get
      Set(value As String)
          ViewState("MemberID2Selection") = value
      End Set
  End Property
  
  Protected Property MemberID3Selection() As String
      Get
          If ViewState("MemberID3Selection") IsNot Nothing Then
              Return ViewState("MemberID3Selection").ToString()
          End If
          Return ""
      End Get
      Set(value As String)
          ViewState("MemberID3Selection") = value
      End Set
  End Property
  
  
  Protected Sub FillddlLocations()
      FillDropdown(companyID1)
      FillDropdown(companyID2)
      FillDropdown(companyID3)
  
      companyID1.Visible = True
      companyID2.Visible = True
      companyID3.Visible = True
  
  End Sub
  
  Protected Sub FillDropdown(ddl As DropDownList)
  
      Using connAdd = New SqlConnection("Data Source = MENDOZAABBY-PC\SQLEXPRESS; Initial Catalog = ThesisDatabase; Integrated Security= True")
          connAdd.Open()
  
          Dim sql = "SELECT CompanyName FROM Company Where College = 'CCS'"
          Using cmdAdd = New SqlDataAdapter(sql, connAdd)
              Dim ds2 As New DataSet()
              cmdAdd.Fill(ds2)
  
  
              ddl.Items.Clear()
              ddl.DataSource = ds2
              ddl.DataTextField = "CompanyName"
              ddl.DataValueField = "CompanyName"
              ddl.DataBind()
              ddl.Items.Insert(0, New ListItem("Please select a Company", ""))
  
              ddl.SelectedIndex = 0
  
          End Using
      End Using
  End Sub
  
  Protected Sub IndexChanged(ddlChanged As DropDownList, ddlToFilter1 As DropDownList, ddlToFilter2 As DropDownList)
      Dim removeValue1 As String = If(ddlChanged Is companyID1, MemberID1Selection, (If(ddlChanged Is companyID2, MemberID2Selection, MemberID3Selection)))
      Dim selValue2 As String = If(ddlChanged Is companyID1, MemberID2Selection, (If(ddlChanged Is companyID2, MemberID1Selection, MemberID1Selection)))
      Dim selValue3 As String = If(ddlChanged Is companyID1, MemberID3Selection, (If(ddlChanged Is companyID2, MemberID3Selection, MemberID2Selection)))
  
      bFlag = False
      'Prevent fireing the code again while changing the index
      If removeValue1 &lt;&gt; "" Then
          Dim item1 As ListItem = ddlToFilter1.Items.FindByValue(removeValue1)
          ddlToFilter1.Items.Remove(item1)
          Dim item2 As ListItem = ddlToFilter2.Items.FindByValue(removeValue1)
          ddlToFilter2.Items.Remove(item2)
  
      End If
  
      If selValue3 &lt;&gt; "" Then
          Dim item3 As ListItem = ddlToFilter1.Items.FindByValue(selValue3)
          ddlToFilter1.Items.Remove(item3)
      End If
      If selValue2 &lt;&gt; "" Then
          Dim item4 As ListItem = ddlToFilter2.Items.FindByValue(selValue2)
          ddlToFilter2.Items.Remove(item4)
      End If
  
      bFlag = False
      ddlToFilter1.SelectedIndex = ddlToFilter1.Items.IndexOf(ddlToFilter1.Items.FindByValue(selValue2))
      ddlToFilter2.SelectedIndex = ddlToFilter2.Items.IndexOf(ddlToFilter2.Items.FindByValue(selValue3))
  
  End Sub
  
  Protected Sub ddlpid1_SelectedIndexChanged(sender As Object, e As EventArgs)
      MemberID1Selection = companyID1.SelectedValue
      If bFlag Then
          FillDropdown(companyID2)
          FillDropdown(companyID3)
          IndexChanged(companyID1, companyID2, companyID3)
      End If
  End Sub
  
  Protected Sub ddlpid2_SelectedIndexChanged(sender As Object, e As EventArgs)
      MemberID2Selection = companyID2.SelectedValue
      If bFlag Then
          FillDropdown(companyID1)
          FillDropdown(companyID3)
          IndexChanged(companyID2, companyID1, companyID3)
      End If
  End Sub
  Protected Sub ddlpid3_SelectedIndexChanged(sender As Object, e As EventArgs)
      MemberID3Selection = companyID3.SelectedValue
      If bFlag Then
          FillDropdown(companyID1)
          FillDropdown(companyID2)
          IndexChanged(companyID3, companyID1, companyID2)
      End If
  End Sub
  </code></pre>
  
  <p>End Class</p>
  
  <hr>
  
  <p><strong>ASPX</strong></p>
  
  <pre><code>&lt;%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
      &lt;asp:DropDownList ID="companyID1" AutoPostBack="true" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid1_SelectedIndexChanged"&gt;
      &lt;/asp:DropDownList&gt;
      &lt;asp:DropDownList ID="companyID2" AutoPostBack="true" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid2_SelectedIndexChanged"&gt;
      &lt;/asp:DropDownList&gt;
      &lt;asp:DropDownList ID="companyID3" AutoPostBack="true" runat="server" Visible="false" OnSelectedIndexChanged="ddlpid3_SelectedIndexChanged" &gt;
      &lt;/asp:DropDownList&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>