Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Clone listview or filter them

Posted on 2006-06-19
10
Medium Priority
?
585 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
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
 
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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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