search DataGridView?

rjhe22

Well-known member
Joined
May 22, 2008
Messages
88
Programming Experience
Beginner
hi can anyone help seriously stuck. i have a DataGridView which pull info from a database. now i need to be able to search this DataGridView generically. has anyone any idea how this can be done
 
You mean you wish to look in the DGV for some value?

I'd enumerate the column names and (for the string types) provide them in a combo box
The user would choose one and then provide a string they wished to look for
You would then set the .Filter of the DGV's BIndingSource to:

VB.NET:
.Filter = string.Format("[{0}] LIKE '%{1}%'", cboColumn.Text, txtSrchTerm.Text)
 
have u done something like this before. any chance u could post the code up so i could have a look. a bit confused on the whole thing
 
I code in C#, and I've never done this before, no

Might I suggest you start simply.. Instead of the combo, use two text boxes, get the user to write the name of the column they are searching, and then in the other box, write the value they are searhcing
 
im not using a combo box im just using a text box and when i enter letters it picks it up as im going along till it comes to a match. i thought u did code in vb.net. thanks for all the help just so confused with the whole thing
 
thats the thing i dont really have any code keep going around in circles. putting code in and taking it out that why i was looking for something to look at. to figure out what exactly i need. when i get it i will post the code up
 
Well.. it's true that weeks of coding can save you hours of planning.

Stop coding and sit down with a pencil and paper and work out what you want to do

read about the .Filter property of the bindingsource (you should be using a binding source) in MSDN
Read about String.Format
Write some simple iunstructions in your native language. type them up as comments
Put the code in afterwards

Even pro programmers do this!
 
this is the code i used to get this working

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
 
now i could do with another hand i have these few lines of code i need to get this search working and i need no no does anyone no were i can put it so it loads so the search works

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
 
Back
Top