Remove duplicates in listview in VB.NET

M

Mamatha

Hi

I have an application with listview.When i click on one
button the data will be displayed like this in the
listview:
colA colB colC
----- ----- ------
nannacom.com 0 0

When i click on another button,i want to display like this
in the same list view:
colA colB colC
----- ----- ------
nannacom.com 2 5

How can i replace the first one with the second one.
I displayed data into listview from database.

If any one knows the solution please let me know.
Thakns in advance.

Mamatha
 
G

Guest

You could create separate Datatables for the data you want displayed from
each Button click. The following method comes from a project I recently
completed.

Parameter Info:

ListView is the ListView you are filling. DataTable provides the data.
Columns is an ArrayList that provides the names of the fields from the
Datatable that you want to include in the ListView. If you know that you
would always want to use all fields, you could remove the ArrayList
parameter, and modify the code.

Public Sub FillListView(ByVal ListView As ListView, _
ByVal DataTable As DataTable, ByVal FieldNames As ArrayList)
ListView.Items.Clear()
Dim i As Int32
Dim j As Int32
Dim LVI As ListViewItem
Do Until i = DataTable.Rows.Count
j = 0
Dim s(FieldNames.Count - 1) As String
Do Until j = FieldNames.Count
If Not
IsDBNull(DataTable.Rows(i).Item(FieldNames(j).ToString)) Then
s(j) =
DataTable.Rows(i).Item(FieldNames(j).ToString).ToString
Else
s(j) = ""
End If
j += 1
Loop
LVI = New ListViewItem(s)
ListView.Items.Add(LVI)
i += 1
Loop
End Sub


www.charlesfarriersoftware.com
 
T

thomas wenning

Charlie said:
You could create separate Datatables for the data you want displayed from
each Button click. The following method comes from a project I recently
completed.

Parameter Info:

ListView is the ListView you are filling. DataTable provides the data.
Columns is an ArrayList that provides the names of the fields from the
Datatable that you want to include in the ListView. If you know that you
would always want to use all fields, you could remove the ArrayList
parameter, and modify the code.

Public Sub FillListView(ByVal ListView As ListView, _
ByVal DataTable As DataTable, ByVal FieldNames As ArrayList)
ListView.Items.Clear()
Dim i As Int32
Dim j As Int32
Dim LVI As ListViewItem
Do Until i = DataTable.Rows.Count
j = 0
Dim s(FieldNames.Count - 1) As String
Do Until j = FieldNames.Count
If Not
IsDBNull(DataTable.Rows(i).Item(FieldNames(j).ToString)) Then
s(j) =
DataTable.Rows(i).Item(FieldNames(j).ToString).ToString
Else
s(j) = ""
End If
j += 1
Loop
LVI = New ListViewItem(s)
ListView.Items.Add(LVI)
i += 1
Loop
End Sub

Hi Charlie, hi Mamatha,

Charlie, i think you fill the list only, here is the solution:

Private Sub RemoveDoubleEntries(ByVal LBox As ListBox)
Dim Entries1() As String
Dim Entries2() As String
Dim UB As Integer
Dim i1 As Integer
Dim i2 As Integer
Dim found As Boolean
Dim DidInit As Boolean

'Array(Data) dim the array
ReDim Entries1(LBox.Items.Count)

'List read
For i1 = 0 To LBox.Items.Count - 1
Entries1(i1) = LBox.Items.Item(i1)
Next

'All entries
For i1 = 0 To UBound(Entries1)
'Flag set
found = False

'read all entries
For i2 = 0 To UB
'Init?
If Not DidInit Then
'Wen found init a little later
found = False
Exit For
ElseIf Entries1(i1) = Entries2(i2) Then
'Double Item found!
found = True
Exit For
End If
Next

'is a double Item?
If Not found Then
'Is init done?
If Not DidInit Then
'Neue Liste auf erforderliche Größe setzen
ReDim Entries2(UBound(Entries1))
DidInit = True
End If

'Add to the new list
Entries2(UB) = Entries1(i1)

UB = UB + 1
End If
Next

'once to much
UB = UB - 1
'Restlichen Elemente abschneiden.
ReDim Preserve Entries2(UB)

'Fill listview with the new list
LBox.Items.Clear()
For i1 = 0 To UB
LBox.Items.Add(Entries2(i1))
Next
End Sub

Greeting

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top