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
Solved

Clone listview or filter them

Posted on 2006-06-19
10
581 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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