Solved

Clone listview or filter them

Posted on 2006-06-19
10
579 Views
Last Modified: 2008-03-17
I have a listview that I need to filter based on a combobox.  The combobox text is associated with the tag for each row.  I'd like to limit what is shown in the listview based on the combobox item selected.

Now I looked around briefly for a way to "filter" the listview but didn't find anything so I was going to try somethign different.

When I populate the listview I want to create a clone/copy of it.  Then everytime the combobox index changes the original would replace the listview, and I would delete every row with a different tag from the combobox text.

::shrug::

But upon playing with this idea, I have not found a way to clone the listview to preserve the original data.

Help please.
0
Comment
Question by:Brothernod
  • 5
  • 5
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16945886
You could bind the ListView's to a DataView, and then apply a filter expression to the DataView:

  Dim view1 As New DataView(Me.DataSet1.Tables(0))
  Dim view2 As New DataView(Me.DataSet1.Tables(0).Copy)

  view2.RowFilter = "Field1 = '" & value & "'"
 
Bob
0
 
LVL 1

Author Comment

by:Brothernod
ID: 16946130
This idea sounds very promising.  What you are saying is that I should.

Populate a dataview from my database.
Bind the dataview to the listview.
On Combobox index changed dataview.filter.

That way I don't need to rebind anything or stuff of that nature?

Ok, now for my nice ignorant followup.

I currently populate my listview with a While DataReader.Read loop where I step through each item in the datareader and listview.items.add .... i assume you know what I mean.

How do I do this with a dataview, i've never worked with one.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16946237
1) A DataReader is a light-weight, forward-only view of the data

2) A DataTable has a lot more to it, but takes up more memory

3) If you can deal with the overhead of a DataTable, it comes with more capability, such as the Copy method, to clone the structure and data.

4) If you are using a DataTable/DataView combination, you can iterate over each item like this:

   For Each drv As DataRowView In view1
      Dim value As String = drv("Column1").ToString()
   Next drv

Bob
0
 
LVL 1

Author Comment

by:Brothernod
ID: 16946746
Thanks for clarifying.

I've been playing with it for a bit now.  What I have currently is that I fill a dataset from my database.  I then wrote a function (for code cleanliness) to populate a listview from a dataset.

I'm currently working on how to filter the dataset before passing it to the FillListView function.

And yes, for these listviews i'll have at most 50 records, so it's not really a concern about memory overhead, it's a LOT faster than hitting the database for each refresh.

The reason I used a dataset is that the book i've read through to learn most of this provided a nice little class with a ds fill function.

Should I attempt to dump the table from the DS into a dataview or datatable?
0
 
LVL 1

Author Comment

by:Brothernod
ID: 16946981
I attempted to filter in this manner.

I created a dataview from the table in the dataset.
I set the rowfilter for the dataview.

Problem is that it doesn't seem to be working.


Dim dvw as DataView
dvw = New DataView(dsCurrent.Tables(0))
dvw.RowFilter = "bAccount = '" & cmboWAccount.Text & "'"

after the rowfilter statement there are still 2 results when there should be 1 in dvw.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 16953298
Did you make sure that you have the correct results in dsCurrent.Tables(0)?  Is there an incorrect select statement that would duplicate records?

Bob
0
 
LVL 1

Author Comment

by:Brothernod
ID: 16963841
Here is my code.

On cmboWAccount combobox changed section :

Dim dvw As DataView
dvw = New DataView(dsCurrent.Tables(0))
dvw.RowFilter = "bAccount = '" & cmboWAccount.Text & "'"
FillListView(lvwWCurrentMonth, dvw, 4)
dvw = New DataView(dsNext.Tables(0))
dvw.RowFilter = "bAccount = '" & cmboWAccount.Text & "'"
FillListView(lvwWNextMonth, dvw, 4)
dvw = Nothing

_________________
Private Sub FillListView(ByRef lv As ListView, ByVal dvw As DataView, Optional ByVal intDateColumn As Integer = Nothing)
        Dim objListViewItem As ListViewItem
        Dim shtFieldCntr As Short
        If intDateColumn <> Nothing Then intDateColumn = intDateColumn - 1
        lv.Items.Clear()
        For Each dr As DataRow In dvw.Table.Rows
            objListViewItem = New ListViewItem
            objListViewItem.Text = dr(0)
            objListViewItem.Tag = dr(1)
            For shtFieldCntr = 2 To dvw.Table.Columns.Count - 1
                If shtFieldCntr = intDateColumn Then
                    Dim strLastMaintDate As String
                    If dr(intDateColumn).ToString = "" Then
                        strLastMaintDate = ""
                    Else
                        strLastMaintDate = Format(dr(intDateColumn), "MM/dd/yyyy")
                    End If
                    objListViewItem.SubItems.Add(strLastMaintDate)
                Else
                    objListViewItem.SubItems.Add(dr(shtFieldCntr).ToString)
                End If
            Next
            lv.Items.Add(objListViewItem)
        Next
    End Sub
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 300 total points
ID: 16963902
There's your trouble:

   For Each dr As DataRow In dvw.Table.Rows

The Table property doesn't mean what you think it does.  It returns a reference to the underlying DataTable instance, with all the records and no filter applied.  

To get the rows in a DataView, you need something like this:

   For Each dr As DataRowView In dvw

Bob
0
 
LVL 1

Author Comment

by:Brothernod
ID: 16965233
You sir r0ck.

What do you do for a living?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16965759
Program in VB.NET, C#, ASP.NET, WinForms, WMI, ActiveDirectory, JavaScript, AJAX, VBScript, etc.

Bob
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now