Solved

Clone listview or filter them

Posted on 2006-06-19
10
584 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

622 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