searching datagrid

rjhe22

Well-known member
Joined
May 22, 2008
Messages
88
Programming Experience
Beginner
this is the code i used to search a datagrid

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged

Dim strFilter As String = ""


For Each col As DataColumn In Me._main.Columns
If Me.grdTask.Columns(col.ColumnName).Visible = True Or Not String.IsNullOrEmpty(col.Expression) Then

Dim strClause As String = "CONVERT(" & col.ColumnName & ",'System.String') LIKE '*" & Me.txtSearch.Text & "*'"
If strFilter = "" Then
strFilter &= strClause
Else
strFilter &= " OR " & strClause
End If

End If

Next

Me.bndTask.Filter = strFilter



End Sub



but i need this part of the code as well to get the search working correctly
Me._datset.Relations.Add(TableName & "_" & Me._main.TableName, _
tbl.Columns(IDColumn), Me._main.Columns(IDColumn))

Dim datCol As New DataColumn(DisplayColumn)
datCol.DataType = tbl.Columns(DisplayColumn).DataType
datCol.Expression = "Parent(" & TableName & "_" & Me._main.TableName & ")." & DisplayColumn
Me._main.Columns.Add(datCol)
Me.grdMain.Columns(DisplayColumn).Visible = False


any one have anyidea what the best way to do this is
 
VB.NET:
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged

Dim strFilter As String = ""


For Each col As DataColumn In Me._main.Columns
If Me.grdTask.Columns(col.ColumnName).Visible = True Or Not String.IsNullOrEmpty(col.Expression) Then

Dim strClause As String = "CONVERT(" & col.ColumnName & ",'System.String') LIKE '*" & Me.txtSearch.Text & "*'"
If strFilter = "" Then
strFilter &= strClause
Else
strFilter &= " OR " & strClause
End If

End If

Next

Me.bndTask.Filter = strFilter



End Sub





VB.NET:
Me._datset.Relations.Add(TableName & "_" & Me._main.TableName, _
tbl.Columns(IDColumn), Me._main.Columns(IDColumn))

Dim datCol As New DataColumn(DisplayColumn)
datCol.DataType = tbl.Columns(DisplayColumn).DataType
datCol.Expression = "Parent(" & TableName & "_" & Me._main.TableName & ")." & DisplayColumn
Me._main.Columns.Add(datCol)
Me.grdMain.Columns(DisplayColumn).Visible = False
 
sorry forgot to put the question in

ya the first part is the code i want to put in for searching

the second part is related to the first but i need to get the first part to pick up the second part if u no what i mean so the search runs right.

any ideas
 
Sorry.. i'm still not really clear on what you are looking to do. If the user types a string, you want to search every cxell of the grid for that string and show only rows containing a cell that contains that string?
 
yes that is sorry im very bad at explaining myself.

so if i type in john in my textbox i and one of my combobox are users i want it to find the name.
 
VB.NET:
'YOU NEED TO ADD A COLUMN TO YOUR DATATABLE CALLED ShowInGrid, TYPE Boolean

For Each r as MyDataRow in MyDataSet.MyDataTable
  For Each o as Object in r.ItemArray
    r.ShowInGrid = (o IsNot Nothing AndAlso o.ToString().Contains(searchTextBox.Text) )
  Next o
Next r

myBindingSource.Filter = "[ShowInGrid] = True"
 
and does that go into
VB.NET:
    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
 
Oh for the love of god, no!

Do you really want to scan EVERY cell of EVERY row in the table EVERY time the user presses a key?

Thought not.. Wire it up to a button click.

But you do want it, then accept that your UI is gonna be SLOWWWWWW
 
ok thats grand thank for all the help. u seem to no alot about it.its not the way i was asked to do it so i will have to figure out another way
 
its not the way i was asked to do it so i will have to figure out another way

Well.. thanks for taking the minutes out of my life to show you something that cannot be used to solve your problem. Good luck! :wave:
 

Latest posts

Back
Top