Hi
How to search the text in particular column in data grid view
thanks
Printable View
Hi
How to search the text in particular column in data grid view
thanks
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.
Hi,
populate the grid
trying the searching the grid value: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
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
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.
pls can u give me the detail description with code
To search the data in column .. i try to bound the data grid view
error: BindingSource cannot be its own data source. Do not set the DataSource and DataMember properties to values that refer back to BindingSource.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
What did I say previously?You've got the first half of that right.Quote:
bind your DataTable to a BindingSource and the BindingSource to the grid
now it is ok
now tell me how to search the dataCode:dgvDisplayBindingSource.DataSource = DtDisplay
dgvDisplay.DataSource = dgvDisplayBindingSource
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.
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
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:Alternatively, you could set the Filter property of the BindingSource and all rows that don't match will be filtered out of grid.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