|
-
Nov 30th, 2012, 03:37 AM
#1
Thread Starter
Lively Member
[RESOLVED] How to filter listview contents as you type in a text box
Hi? I need help in filtering contents in a listview. The list view has several columns but the user is supposed to filter by selecting either "Serial Number" or "Buyer Name" column from a combo box. When the user starts typing in a search textbox, the contents are filtered automatically without hitting any button. The list view contents are from access database and here is my connection code..
Code:
Imports System.Data.OleDb
Module Module1
Public Conn As New OleDbConnection
Public MyCmd As New OleDbCommand
Public OleDBDtRdr As OleDbDataReader
Public MyQuerry As String = Nothing
Sub ConnectionToDatabase()
Try
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=PowerMiss.accdb"
.Open()
End With
Catch ex As Exception
MsgBox("Unable to connect to database.", MsgBoxStyle.Exclamation, "Connection Error!")
End Try
End Sub
End module
And here is how I have porpulated the list view
Code:
Public Class frmSoldUPS
Private Sub frmSoldMachines_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
cmbSearch.Items.Add("Serial Number")
cmbSearch.Items.Add("Buyer Name")
If Conn.State = ConnectionState.Closed Then
MsgBox("Please login first to connect to database.", MsgBoxStyle.Information, "Unable to connect to database.")
Exit Sub
End If
With lvSoldMachines.Columns
.Add("ID", 25, HorizontalAlignment.Left)
.Add("Buyer Name", 150, HorizontalAlignment.Left)
.Add("Buyer location", 100, HorizontalAlignment.Left)
.Add("Branch", 100, HorizontalAlignment.Left)
.Add("RQ No.", 75, HorizontalAlignment.Left)
.Add("DN No.", 75, HorizontalAlignment.Left)
.Add("Date purchased", 100, HorizontalAlignment.Left)
.Add("UPS Brand and Model", 125, HorizontalAlignment.Left)
.Add("Rating 'KVA'", 80, HorizontalAlignment.Left)
.Add("Phases In", 75, HorizontalAlignment.Left)
.Add("Phases out", 75, HorizontalAlignment.Left)
.Add("Serial Number", 100, HorizontalAlignment.Left)
.Add("Purpose", 100, HorizontalAlignment.Left)
End With
FillListView()
End Sub
Public Sub FillListView()
lvSoldMachines.Items.Clear()
MyQuerry = "SELECT * from tblBuyers ORDER BY id ASC"
MyCmd = New OleDbCommand(MyQuerry, Conn)
OleDBDtRdr = MyCmd.ExecuteReader
While OleDBDtRdr.Read
With lvSoldMachines
.Items.Add(OleDBDtRdr("ID"))
With .Items(.Items.Count - 1).SubItems
.Add(OleDBDtRdr("BuyerName"))
.Add(OleDBDtRdr("TownOrLocation"))
.Add(OleDBDtRdr("Branch"))
.Add(OleDBDtRdr("RequisitionNumber"))
.Add(OleDBDtRdr("DeliveryNoteNumber"))
.Add(OleDBDtRdr("DateOfPurchase"))
.Add(OleDBDtRdr("BrandAndModel"))
.Add(OleDBDtRdr("KVARating"))
.Add(OleDBDtRdr("PhasesIn"))
.Add(OleDBDtRdr("PhasesOut"))
.Add(OleDBDtRdr("MachineSerialNumber"))
.Add(OleDBDtRdr("SaleOrStandby"))
End With
End With
End While
End Sub
End class
Sorry, if I have given more than required information, but please correct me if something doesn't look ok.
-
Nov 30th, 2012, 04:19 AM
#2
Re: How to filter listview contents as you type in a text box
I always find it mind-boggling the number of people who make their life significantly more difficult by using a ListView when they should be using a DataGridView. With the proper grid, you simply use a data adapter to populate a DataTable, bind that to a BindingSource and bind that to the grid. You then filter with a single line of code by setting the Filter property of the BindingSource. Trying to filter a ListView is just a bad idea.
-
Nov 30th, 2012, 04:52 AM
#3
Thread Starter
Lively Member
Re: How to filter listview contents as you type in a text box
Since I already made my life difficult, what should I do? I can't go back and start from scratch but to complete my mess. I tried the link http://social.msdn.microsoft.com/For...-ce0430de0c3f/ but it didn't help much, maybe it will be helpful somewhere else and others who will read it.
-
Nov 30th, 2012, 05:31 AM
#4
Re: How to filter listview contents as you type in a text box
You don't have start again from complete scratch but you should definitely get rid of the ListView and add a DataGridView instead. Like I said, it's barely any code to use a DataGridView in this way so you're going to be doing mostly deleting of old code. It's going to save you time in the long run.
-
Nov 30th, 2012, 05:35 AM
#5
Thread Starter
Lively Member
Re: How to filter listview contents as you type in a text box
Okay, Let me start on that immediately..
-
Dec 28th, 2012, 09:36 AM
#6
Thread Starter
Lively Member
Re: How to filter listview contents as you type in a text box
Thanks for the advice... Datagridview deed the trick...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|