Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filter from Text Box

Status
Not open for further replies.

dedo8816

Programmer
Oct 25, 2006
94
GB
Hi folks,

How can I change this code to work with partial search criteria?

This basically looks through the lines in a dataview and goes to the matching line from the search in txtfind.txt.
What I would like to be able to do here is to change the below to work as a filter instead with either a partial search criteria or maybe even use a wildcard in the middle i.e. "ms*1-07" will display the full search "ms27039-1-07"... Im currently at a loss as im still a novice at this...

Dim dt As DataTable = CType(Me.CrossRefDataGridView.DataSource, DataTable)
Dim cm As CurrencyManager = Me.BindingContext(dt)
Dim dr As DataRow
'CrossRefDataGridView.Sort(CrossRefDataGridView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
If RBCross.Checked Then
If dt.Select("[Cross-Reference No] ='" & (TxtFind.Text) & "'").Length > 0 Then
dr = dt.Select("[Cross-Reference No] ='" & (TxtFind.Text) & "'")(0)
Else
dr = Nothing
End If

Try
dr = dt.Select("[Cross-Reference No] ='" & (TxtFind.Text) & "'")(0)
Catch ex As Exception

End Try
If Not dr Is Nothing Then
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i) Is dr Then
cm.Position = i
Exit For
End If
Next
Else
MessageBox.Show("row not found")
End If
dt = Nothing
cm = Nothing
dr = Nothing
End If


Thanks for your help

Dedo
 
There are several differenct ways to do this using a combination of a dataview and or a bindingsource. This is the way I always do it. It may be a little overkill, but it gives me complete flexibility. First I would do away with the currencymanager and use a bindingsource. From what I can tell the currencymanager is older tecnhology and the bindingsource seems to be favored.
1) I always use a dataview instead of directly using the datatable as my datasource. The dataview is basically a query with robust features for sorting, filtering, and searching. It returns an array of datarowview objects. Any changes or updates to these change the underlying datatable. (It is one level of abstraction).
2) So you can fill your datatable, create a dataview from that table, and then bind that to your control/s. However, I add another layer of abstraction and use a bindingsource. People will argue that this is not necesssary, but for me it exposes certain properties and methods that come in handy. Specifally the current property. It has a sort and filter property. So in truth you could actually do it this way.
Datatable is datamember of the bindingsource, and the bidingsource is the datasource for your datagridview.
Then you can just filter the bindingsource
I do the overkill and
relate the dataview to the datable, relate the dataview to the bindingsource, and bind the datagridview to the bindingsource.

So your options are
make a dataview and bind that to the datagridview without a bindingsource (filter the dataview)
relate the datatable to the bindingsource and bind that to the datagridview (filter the bindingsource)
or chain all three together.

Bottom line: Do away with the currencymanager and use a bindingsource. Read up on dataview and bindingsource, and they can be used in different combinations.
 
So the more I read, there is no need to create a dataview as well. When you associate the datatable to the bindingsource it actually uses a dataview. All datatables have a defaultview property. So simply use a bindingsuource, associate it to your table, and then you can easily filter and sort.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top