Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 586
  • Last Modified:

Clone listview or filter them

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
Brothernod
Asked:
Brothernod
  • 5
  • 5
1 Solution
 
Bob LearnedCommented:
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
 
BrothernodAuthor Commented:
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
 
Bob LearnedCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BrothernodAuthor Commented:
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
 
BrothernodAuthor Commented:
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
 
Bob LearnedCommented:
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
 
BrothernodAuthor Commented:
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
 
Bob LearnedCommented:
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
 
BrothernodAuthor Commented:
You sir r0ck.

What do you do for a living?
0
 
Bob LearnedCommented:
Program in VB.NET, C#, ASP.NET, WinForms, WMI, ActiveDirectory, JavaScript, AJAX, VBScript, etc.

Bob
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now