Quantcast
Channel: AuthorCode » ListBox control
Viewing all articles
Browse latest Browse all 9

How to remove duplicate items from listbox in .net

$
0
0

 
Many times when we bind our controls from a non primary column of table then it may be a case that we got the duplicate records in our dataset. There is no sense to bind duplicate records without any identity value in any control.

With the help of the following function you can easily remove duplicate items from your Listbox control.

[VB]

  1.  
  2. Private Sub RepoveDuplicate()
  3.         For Row As Int16 = 0 To MyListBox.Items.Count - 2
  4.             For RowAgain As Int16 = MyListBox.Items.Count - 1 To Row + 1 Step -1
  5.                 If MyListBox.Items(Row).ToString = MyListBox.Items(RowAgain).ToString Then
  6.                     MyListBox.Items.RemoveAt(RowAgain)
  7.                 End If
  8.             Next
  9.         Next
  10.     End Sub

[C#]

  1.  
  2. private void RepoveDuplicate()
  3. {
  4.  for (Int16 Row = 0; Row <= MyListBox.Items.Count - 2; Row++) {
  5.   for (Int16 RowAgain = MyListBox.Items.Count - 1; RowAgain >= Row + 1; RowAgain += -1) {
  6.    if (MyListBox.Items(Row).ToString == MyListBox.Items(RowAgain).ToString) {
  7.     MyListBox.Items.RemoveAt(RowAgain);
  8.    }
  9.   }
  10.  }
  11. }

Viewing all articles
Browse latest Browse all 9

Trending Articles