|
-
Oct 3rd, 2007, 01:21 AM
#1
Thread Starter
Hyperactive Member
Data grid view - search
Hi
How to search the text in particular column in data grid view
thanks
-
Oct 3rd, 2007, 01:27 AM
#2
Re: Data grid view - search
Is this grid bound to a DataTable or some other data source? If so then you'd search the data source via a BindingSource, not the grid. If it's not bound then you simply loop through the rows and test the value in the column of interest each iteration.
-
Oct 3rd, 2007, 02:46 AM
#3
Thread Starter
Hyperactive Member
Re: Data grid view - search
Hi,
populate the grid
Code:
Private Sub PopulateSearchGrid1()
Dim DaSearch As SqlDataAdapter
Dim dtSearch As New DataTable
Dim StrQuery As String
Try
StrQuery = "SELECT EmployeeMaster.EmpCode, EmployeeMaster.Name as EmpName,EmployeeMaster.DOB FROM EmployeeMaster"
DaSearch = New SqlDataAdapter(StrQuery, Conn)
DaSearch.Fill(dtSearch)
dgvSearch.DataSource = dtSearch
DaSearch.Dispose()
dtSearch.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
trying the searching the grid value:
Code:
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
Dim intLoop As Integer
Dim intLength As Integer
Dim intRowFound As Integer = 0
intLength = Len(txtSearch.Text)
If dgvSearch.RowCount > 1 Then
For intLoop = 1 To dgvSearch.RowCount - 1
If LCase(txtSearch.Text) = LCase(Mid(dgvSearch.Rows(intLoop).Cells("EmpName").Value, 1, intLength)) Then
intRowFound = intLoop
'dgvSearch.Row = intRowFound
'dgvSearch.TopRow = intRowFound
Exit Sub
Else
'dgvSearch.Row = intRowFound
'dgvSearch.TopRow = intRowFound
End If
Next
End If
End Sub
-
Oct 3rd, 2007, 03:03 AM
#4
Re: Data grid view - search
So it's bound. In that case you should bind your DataTable to a BindingSource and the BindingSource to the grid, instead of the table directly to the grid. You can then call the Find method of the BindingSource to get the index of the first matching row.
-
Oct 3rd, 2007, 07:32 AM
#5
Thread Starter
Hyperactive Member
Re: Data grid view - search
pls can u give me the detail description with code
-
Oct 4th, 2007, 01:59 AM
#6
Thread Starter
Hyperactive Member
Re: Data grid view - search
To search the data in column .. i try to bound the data grid view
Code:
Private Sub PopulateDisplay()
Dim dgvDisplayBindingSource As New BindingSource()
Dim DtDisplay As New DataTable
Dim StrDisplay As String = "SELECT EM.Empcode,EM.EmpName, EM.DOB as [Date of Birth],"
StrDisplay = StrDisplay + " DM.DesigName as Designation, EM.Salary FROM EmployeeMaster EM"
StrDisplay = StrDisplay + " LEFT JOIN DesignationMaster DM ON EM.Desigcode =dm.DesigCode"
Dim Dadisplay As New OleDbDataAdapter(StrDisplay, Conn)
Dadisplay.Fill(DtDisplay)
Try
dgvDisplayBindingSource.DataSource = DtDisplay
dgvDisplayBindingSource.DataSource = dgvDisplayBindingSource
'dgvDisplay.DataSource = DtDisplay
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
error: BindingSource cannot be its own data source. Do not set the DataSource and DataMember properties to values that refer back to BindingSource.
-
Oct 4th, 2007, 02:09 AM
#7
Re: Data grid view - search
What did I say previously?
bind your DataTable to a BindingSource and the BindingSource to the grid
You've got the first half of that right.
-
Oct 4th, 2007, 03:01 AM
#8
Thread Starter
Hyperactive Member
Re: Data grid view - search
now it is ok
Code:
dgvDisplayBindingSource.DataSource = DtDisplay
dgvDisplay.DataSource = dgvDisplayBindingSource
now tell me how to search the data
-
Oct 4th, 2007, 04:03 AM
#9
Re: Data grid view - search
I suggest that you read the documentation for the BindingSource class first, so you understand what it is and what it does, then post back if you are still in need.
-
Oct 4th, 2007, 11:41 PM
#10
Thread Starter
Hyperactive Member
Re: Data grid view - search
Hi,
According to your guide line .. I try and result will be achived but i want to display the grid according to the value type in text box means..
if i type p if any word start with p than cursor will move to that row..
Code:
Private Sub txtSearchName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearchName.TextChanged
Try
Dim FoundIndex As Integer = dgvDisplayBindingSource.Find(Trim(lblSearch.Text), txtSearchName.Text)
'dgvDisplay.Rows(FoundIndex).Cells(Trim(lblSearch.Text)).Value
If FoundIndex > -1 Then
MsgBox("Record Found...")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
-
Oct 4th, 2007, 11:56 PM
#11
Re: Data grid view - search
The Find method takes two parameters: the name of the column to search on and the value of the column to search for. Are those the values you're passing? Also, Find will only look for exact matches. It's no use if you want to find a row where the column value starts with a particular substring.
In that case you should use the Select method of the bound DataTable, which will allow you to filter using wildcards. Once you've got the first matching row you can use the BindingSource.Find method to get the index of the grid row that has the corresponding ID value:
vb.net Code:
Dim rows As DataRow() = myDataTable.Select(String.Format("{0} LIKE '{1}*'", columnName, columnValue), myBindingSource.Sort)
If rows.Length > 0 Then
Dim rowIndex As Integer = myBindingSource.Find("ID = " & rows(0)("ID"))
End If
Alternatively, you could set the Filter property of the BindingSource and all rows that don't match will be filtered out of grid.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|