Click here to Skip to main content
Rate this: bad
good
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:
 
DAProjects.Fill(DSProjects.Tables("Projects"))
 
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)
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 21-Dec-11 22:31pm
sadel132
Edited 22-Dec-11 0:14am
v3
Rate this: bad
good
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#.
 
HTH
  Permalink  
Comments
sadel at 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
Rate this: bad
good
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.
  Permalink  
v2
Comments
sadel at 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 at 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 at 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 at 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 at 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 at 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
0 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 Raul Iloc 170
4 Maciej Los 164
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 22 Dec 2011
Copyright © CodeProject, 1999-2014
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