Click here to Skip to main content
13,055,047 members (61,016 online)
Rate this:
Please Sign up or sign in to vote.
search button for vb 2010 ultimate in access 2007 database and filtering on a datagrid

Hi all!

I create a wpf file and I am trying to make a search button which filters a datagrid. First of all, I have a tab and on this tab a have a datagrid (dgr1). When I load this tab, I fill in a dataset from the database using a data adapter and this code:


and then I send and display the data set to the datagrid with this code:

dgr1.DataContext = DSProjects.Tables("Projects")

Also, I have a TextBox1 and PushButton and when i Push the button I want to diplay on my datagrid only the rows which have the text that I wrote in the textbox.

I try to do this using this code but I don't know how to display (filter) only some rows from the datagrid:

Dim pro As String
Dim MaxProjects As Integer
Dim counterpro As Integer = 0
Dim asd As Integer = -1
MaxProjects = DSProjects.Tables("Projects").Rows.Count
While counterpro < MaxProjects + 1 Or asd = -1

pro = DSProjects.Tables("Projects").Rows(counterpro).Item(1)
asd = pro.IndexOf(TextBox1.Text)
End While

Please explain to me your answer and your code because I don't have so much experience with databases and vb.

Thanx in advance!!!
Posted 21-Dec-11 21:31pm
Updated 21-Dec-11 23:14pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Why don't you create a query via your dataset designer for the table in question that takes in a parameter based on your value in the textbox. Then populate your data grid with parameter query you just created.

There are a ton of MSDN articles on creating parameter queries for both VB.Net and C#.

sadel 22-Dec-11 7:12am
can you please send to me one kilo of articles on creating parameters queries for wpf datagrids and access databases? :P
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

One solution is that as the data source for the data grid you use a DataView[^] and the RowFilter[^] property.
sadel 23-Dec-11 4:21am
thanks for your reply!!!

Do you mean that I can insert this code in the event Button1.Click?

Dim view As DataView = New DataView
With view
.Table = DataSet1.Tables("Suppliers")
.AllowDelete = True
.AllowEdit = True
.AllowNew = True
.RowFilter = "City = 'Berlin'"
.RowStateFilter = DataViewRowState.ModifiedCurrent
.Sort = "CompanyName DESC"
End With

' Simple-bind to a TextBox control
Text1.DataBindings.Add("Text", view, "CompanyName")

Could you please explain me what the last line does mean?
What does this Text1 mean?
Could you please tell me where this code filters my datagrid with the rows which include the text of the TextBox1.Text ????
Mika Wendelius 23-Dec-11 4:34am
When filetering, you should modify the condition in the[^] property based on your text box. For example when the user enters Berlin, the condition would be

.RowFilter = "City = 'Berlin'"

And when user enters Chicago the condition

.RowFilter = "City = 'Chicago'"
sadel 23-Dec-11 4:46am
Yes, but where should I write textBox1.Text to transfer the string which the user enters, to the filter?
Is the Text1 in the last line my textBox1.Text??
Thank you very much for your replies!!!
Mika Wendelius 23-Dec-11 5:03am
You mean something like?

.RowFilter = "City = '" & textBox1.Text & "'"

More elegant version could be:

.RowFilter = string.Format("City = '{0}'", textBox1.Text)
sadel 23-Dec-11 5:10am
Yes and thank you again!!!
But please tell me what is this Text1 in the last line of the code.Please!!!!
Mika Wendelius 23-Dec-11 6:45am
That's the name of the text box control where the databinding is added

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Dec 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100