Re: Datagrid with search capabilities, how to implement it?



Cor and Vasco, thank you for your help. I Think I solved it! The binding
context idea you gave me was the key.

Before getting into details, I need your help with other problem:

I have the FindData form as part of my solution, but as a Class Library
(compiled into a dll). My goal is to keep the main exe as small as possible.
My new problem is that, when I call the dll it runs in its own memory
space... is there a way to make it to run in the main application memory
space? Please remember I am using VBasic .Net 2005.

The datagridview search capabilities problem and solution:
We have a datagridview in a form and want to implement search capabilities
for it.

We create a form FindData with the following controls:

Combobox cbWhere
Contains the list of fields of the datagridview.datasource

Combobox cbMatch
The list of options will vary depending on the data type of the field to
search.
For a string field it will contain three options
Start of field
Any part of field
Whole field
This explanation assumes always a string datatype field

Combobox -> cbWhat
Bound to a datatable where all unique typed criteria are stored

Button Find First
Button Find Next
Button Exit

How does it work?

The calling form (the one with the datagridview) uses a datareader to load
the data into a "GridSource" datatable.

It binds the datagridview to the GridSource datatable.

Sets the datatable.defaultdataview.Sort property to the "Order By" clause of
the SQL command that retrieves the data. The grid datasource will always have
a value for the sort property.

Creates a currencymanager bound to the same binding context of the
datagridview.datasource, using :
myCurrencyManager = CType(Me.BindingContext(GridSource), CurrencyManager)

Sends to the FindData form New() method:
ByRef, The datagridview.datasource
ByVal, the list of fields that can be searched (binary fields and non
visible fields are not passed).
ByRef, the currencymanager

The FindForm New() method:

Uses the list of fields string to fill the cbWhere combobox

Uses two class scoped variables to:
Point to the datagrid.datasource (a datatable variable)
Point to the currencymanager (a currencymanager variable)

When the user :
Chooses the field to search
Chooses the type of matching desired,
Types or selects the string to search,
And clicks on the Find First button:

The program:

Builds the string criteria using
Select Case cbMatch.Text.ToString

Case "Start of field"
strCriteria = "[" + cbWhere.Text.ToString + "] Like '" +
cbWhat.Text.ToString + "*'"

Case "Any part of field"
strCriteria = "[" + cbWhere.Text.ToString + "] Like '*" +
cbWhat.Text.ToString + "*'"

Case "Whole field"
strCriteria = "[" + cbWhere.Text.ToString + "] = '" +
cbWhat.Text.ToString + "'"

End Select

Uses the datatable.Defaultview.ToTable() method to create a new datatable
that has, physically, the same logic order of the original table:

SortTable = mytblGround.DefaultView.ToTable("SortTable", False,
strColumnName, cbWhere.Text.ToString)

Where "SortTable" is the name of the new datatable.
And "strColumnName, cbWhere.Text.ToString" are the names of the two fields
the new datatable will have (the sorting and the searched fields of the
original table).
If the sort property is set to the searched column, the new table will have
only that one field.

Uses the datatable.Select() method on the SortTable to get a datarow array
of matching rows, with the same sorting of the original table:

foundRows = sortTable.Select(strCriteria,
mytblGround.DefaultView.Sort.ToString)

Stores the matching row indexes in an array of integer, using :

ReDim intRows(foundRows.Length - 1)
For x As Integer = 0 To foundRows.Length - 1
intRows(x) = foundRows(0).Table.Rows.IndexOf(foundRows(x))
Next

Disposes SortTable

Sorts the array to ensure a top-bottom accessing order
Array.Sort(intRows)

Sets the currencymanager.Position property to the first row index. Because
of the shared binding context, the movement is done also in the datagridview.

Each time the user clicks the Find Next button, the currencymanager.Position
property is set to the next matching row index.

If the end of the array of integers is reached, the user receives a "No more
matches found" message.


--
Sergio Torres C.
(505) 897 2041
___________________
http://www.stcsys.com
___________________



"Cor Ligthert [MVP]" wrote:

> Sergio,
>
> I did not completely investigate your problem, however if this is the route
> you want to go, than would I eliminate the array by adding a column in the
> datatable. (very easy and there will be nothing updated that does not
> exist). That you can than use in the same way as you do it now. The
> advantage will be that it is than as well in your dataview.
>
> I hope this helps,
>
> Cor
>
>
>
.