Microsoft
Software
Hardware
Network
Question : How do I remove existing ListView items and subitems
Hi,
I'm using VB in VS 2005 to create a reusable ListView display. In my program, the user can select one of four categories of information. The first time the ListView is displayed, all colum names and values are shown correctly. Yet when they select another category, most of the values are missing, even though each value is present in the datatable that I pass to the AddListViewItems routine.
I've tried all of Microsoft's methods such as
Dim instance As ListViewGroupCollection
instance.Clear
but the VB .Net 2005 complier always tells me I need an object, but there is no ListViewGroupCollection method in the standard listview control. Can anyone help me out. Thanks in advance for you thoughts.
Art
Public Sub AddListViewItems(ByVal valuesTable As DataTable)
'''This routine is called numerous time and needs a clean slate each time the user selects another category
Dim cName As String = ""
Dim cValue As String = ""
'Set the view to show details.
lvInformation.View = View.Details
'clear the items and their subitems
For Each lvI As ListViewItem In lvInformation.Items
lvI.SubItems.Clear()
lvInformation.Items.Clear(
)
Next
'clear the columns
lvInformation.Columns.Clea
r()
' Create columns for the items and subitems.
lvInformation.Columns.Add(
"Column", 195, HorizontalAlignment.Left)
lvInformation.Columns.Add(
"Value", 95, HorizontalAlignment.Right)
'add the datatable columns for this single returned row
Dim cCount As Integer = 0
For i As Integer = 0 To valuesTable.Columns.Count - 1
cName = valuesTable.Columns(i).ToS
tring
cValue = valuesTable.Rows(0).Item(i
).ToString
If Not String.IsNullOrEmpty(cValu
e) Then
lvInformation.Items.Add(cN
ame, cCount)
lvInformation.Items(cCount
).SubItems
.Add(cValu
e)
cCount += 1
End If
Next
End Sub
Answer : How do I remove existing ListView items and subitems
But to clarify one thing: column is one thing and group is another. clearing the values from listview doesnt have anything to do with clearing ListViewGroupCollection. So you shouldnt mix these two. Now, to clarify few things:
1) ListView1.Columns.Clear - this one clears columns, but doesnt not clear items in listview. For example, lets say you add 2 columns, and then you add 3 items in listview. If you delete columns, then listview will not display any data on it. But that doesnt mean that there arent any items in listview. On the contrary, there are still 3 items in it, only that in order to show items, columns must exist. So, if you would at this point create again 2 columns, items would appear again.
2) ListView1.Items.Clear - this one clears items from ListVIew, Columns are not affected with it.
3) ListView.Clear - this one clears both items and columns.
Now, in your example, what I would first do is to remove the current ListView control from designer, and add new one. This just to make sure you didnt mess up something with groups. Then you should create columns only once. That means you should remove these lines from AddListViewItems procedure
'Set the view to show details.
lvInformation.View = View.Details
' Create columns for the items and subitems.
lvInformation.Columns.Add(
"Column", 195, HorizontalAlignment.Left)
lvInformation.Columns.Add(
"Value", 95, HorizontalAlignment.Right)
and place them somewhere on the form_load event. Or set these in designer, If you preffer.
Then the code to populate listview would look like this:
Public Sub AddListViewItems(ByVal valuesTable As DataTable)
Dim cName As String = ""
Dim cValue As String = ""
lvInformation.Items.Clear(
)
Dim cCount As Integer = 0
For i As Integer = 0 To valuesTable.Columns.Count - 1
cName = valuesTable.Columns(i).ToS
tring
cValue = valuesTable.Rows(0).Item(i
).ToString
If Not String.IsNullOrEmpty(cValu
e) Then
lvInformation.Items.Add(cN
ame, cCount)
lvInformation.Items(cCount
).SubItems
.Add(cValu
e)
cCount += 1
End If
Next
End Sub
Goran
Random Solutions
How do I check if my Video Card driver is up to date in Gateway 6800-01e
Where does XPS driver print or save the file to and what extention
VFP9R.DLL file is invalid or damaged
Access2003: working with mouse wheel
How Can I Detect, Delete, and Add A Printer In Windows XP using Visual Basic, C#, or C++
Access Server Error 15135
Using TSADMIN.msc is there a way to tell which user is at a desktop, which is using remote app?
problem of do while loop iin foxpro
blinking label?
SSIS DYNamic excel connection string in runtime