Search Inside A DataGrid For Matching Rows

Kannan

Member
Joined
Feb 3, 2005
Messages
13
Location
India
Programming Experience
Beginner
First of all I would like to say that I am a newbie to VB.NET & ADO.NET. For the following problem I searched in google as well as in this forum for "findrows" method and other things which didnt help me.

My problems is, I would like to search inside a data grid and refresh that grid to have rows which matches the search cirteria and display it inside that grid.

This is my code what I have done.

In The Form Load Event
Try
daMain.Fill(dsMain, "tblDigitArchive")

grdMain.SetDataBinding(dsMain, "tblDigitArchive")
grdMain.RetrieveStructure()
grdMain.Refresh()
Catch Eror As Exception
MsgBox(Eror.Message, MsgBoxStyle.Critical, "Error Opening Database")
mConn.Close()
EndTry

Now I request you guys & gals to please post a code by which I can search for multiple rows depending upon an entry from text box inside that same data grid "grdMain" .

Thanks in advance
 
folow these simple steps.

suppose your dataset has been populated and its objdataview

now you would define a dataview object.
dim objdataview as new dataview(objdataset.tables(0))
str is the varibale containing your serach criteria from your textbox

dim dr as datarowcollection()
dr=objdataview.rowfilter("name=" & str )

now your collection object dr is populated with the data acc. to the search criteria filled by the user. you can easily retreive the data from dr.
 
Hey thanks nacracreative, its a 50 % success. I have done the following,


dsMain.Tables("tblDigitArchive").DefaultView.RowFilter = "Title = " & "'" & txtSearch.Text & "'"

grdMain.ClearStructure()

grdMain.SetDataBinding(dsMain.Tables("tblDigitArchive").DefaultView, "")

grdMain.RetrieveStructure()

grdMain.Refresh()


The database has 5 rows Data 1, Title 2, Title 5 (This is not a typo), Data 4, Title 5 under the field Title.

If I give the Row Filter as " Title 5" it gives the two rows which exactly matches to Title 5. This is a 50 % success.

But what I want is, if I give "Title" or "Ti" then the filter should return all rows starting with "Title ", like Title 2, Title 5, Title 5 .
 
In such a case you must not use a rowfilter property of the dataview. instead you must use the sql query string in the commandtext property of sql command object and have parameters collection.

the query string will run like this:

suppose you want to retrieve the names with the text, whose value will saved in @str.

select * from trial where names like '%[' + @str + ']'

in this command @str will be the paramtere object and its value will keep on changing on the basis of insertion of text in the textbox as this code will be called on textbox_change event. now % will server as the wild cahracter. if its inserted in front like in this example, then it will show you all the names ending with the value saved in @str and if its insrted in the end like this

select * from trial where names like '[' + @str + ']%'

then it will show you all the values starting with @str.
 
Last edited:
Hey I got what I was searching.

I replaced the following

dsMain.Tables("tblDigitArchive").DefaultView.RowFilter = "Title = " & "'" & txtSearch.Text & "'"

with the following

dsMain.Tables("tblDigitArchive").DefaultView.RowFilter = "Title like " & "'%" & txtSearch.Text & "%'"
 
Back
Top