Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
5.00/5 (1 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:

VB
DAProjects.Fill(DSProjects.Tables("Projects"))


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

VB
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:


VB
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)
counterpro=counterpro+1
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!!!
sotos
Posted
Updated 21-Dec-11 23:14pm
v3

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#.

HTH
 
Share this answer
 
Comments
sadel 22-Dec-11 7:12am    
can you please send to me one kilo of articles on creating parameters queries for wpf vb.net datagrids and access databases? :P
One solution is that as the data source for the data grid you use a DataView[^] and the RowFilter[^] property.
 
Share this answer
 
v2
Comments
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 ????
Wendelius 23-Dec-11 4:34am    
When filetering, you should modify the condition in the http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx[^] 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!!!
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!!!!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900