none
PROBLEM SEARCH IN DATAGRIDVIEW RRS feed

  • Question

  • HI

    Use this code to search the DATAGRIDVIEW
    He works well with me
    But there is a problem
    When searching if the word has spaces do not search with me

    DATAGRID COLUMN CELL CONTAIN  AHMED

    SEARCH        AH MED NOT SEARCH

    OR SEARCH   AHM ED NOT SEARCH

    SEARCH AHMED  SEARCHGOOD

     Try
                Dim DataAdapter As New OleDbDataAdapter("Select * From TB_WEBSERVERPAYED Where WEBSERVERPAYED_ID LIKE '%" & Trim$(Me.TXT_SEARCHDATGRID.Text) & "%' or WEBSERVERPAYED_CODE LIKE '%" & Trim$(Me.TXT_SEARCHDATGRID.Text) & "%'", con)
    
                con.Open()
                DataSetSEARCH.Clear()
                Me.Cursor = Cursors.WaitCursor
                DataAdapter.Fill(DataSetSEARCH, "TB_WEBSERVERPAYED")
                DATAGRID_SERVER.ClearSelection()
                count()
                If Me.BindingContext(DataSetSEARCH, "TB_WEBSERVERPAYED").Count = 0 Then
    
                    MsgBox("ERRO", 48 + 524288, "SEARCH")
    
                    TXT_SEARCHDATGRID.Text = ""
    
                    con.Close()
    
                    Me.Cursor = Cursors.Default
                    Exit Sub
                Else
    
                    DataSetDG.Clear()
                    DataAdapter.Fill(DataSetDG, "TB_WEBSERVERPAYED")
                    DATAGRID_SERVER.ClearSelection()
                    con.Close()
                    count()
                    Me.Cursor = Cursors.Default
                    Exit Sub
    
                End If
    
    
    
            Catch ex As Exception
    
            End Try


    • Edited by ahmeddc Sunday, September 30, 2018 5:01 PM
    • Changed type ahmeddc Sunday, September 30, 2018 5:34 PM
    Sunday, September 30, 2018 4:58 PM

Answers

  • Hello,

    Here is a clinical example. 

    We want any first name that contains (like) 'ka' or last name contains (like) 'oc'. There are records as follows.

    I placed the code for find in a class and call it as follows 

    Dim ops As New Operations
    ops.FindExample("%ka%", "%oc%")

    Find code in class

    Public Sub FindExample(pFirstName As String, pLastName As String)
        Using cn As New OleDbConnection(Builder.ConnectionString)
            Using cmd As New OleDbCommand("SELECT Id FROM Person WHERE FirstName Like @FirstName OR LastName Like @LastName;", cn)
                cmd.Parameters.AddWithValue("@FirstName", pFirstName)
                cmd.Parameters.AddWithValue("@LastName", pLastName)
                cn.Open()
                Dim reader As OleDbDataReader = cmd.ExecuteReader
                If reader.HasRows Then
                    While reader.Read
                        Console.WriteLine(reader.GetInt32(0))
                    End While
                End If
            End Using
        End Using
    End Sub

    Of course we need to make this work in real life so the first code block changes to this.

    Dim ops As New Operations
    ops.FindExample($"%{txtFirstNameFilter.Text}%", $"%{txtLastNameFilter.Text}%")

    You could do the % signs in the find code also.

    Results

    1
    2
    3
    4
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Sunday, September 30, 2018 7:48 PM
    Sunday, September 30, 2018 7:21 PM
    Moderator

All replies

  • Hi

    You are asking a question I believe - if so please change from Discussion to Question.

    *

    Not sure if this will help, but., if you want to use a String which has spaces in it but without those spaces then:

    Dim a As String = "AH MED"

    Dim s As String = a.Replace(" ", Nothing)

    result: s = "AHMED"


    Regards Les, Livingston, Scotland

    Sunday, September 30, 2018 5:23 PM
  • Hello,

    Here is a clinical example. 

    We want any first name that contains (like) 'ka' or last name contains (like) 'oc'. There are records as follows.

    I placed the code for find in a class and call it as follows 

    Dim ops As New Operations
    ops.FindExample("%ka%", "%oc%")

    Find code in class

    Public Sub FindExample(pFirstName As String, pLastName As String)
        Using cn As New OleDbConnection(Builder.ConnectionString)
            Using cmd As New OleDbCommand("SELECT Id FROM Person WHERE FirstName Like @FirstName OR LastName Like @LastName;", cn)
                cmd.Parameters.AddWithValue("@FirstName", pFirstName)
                cmd.Parameters.AddWithValue("@LastName", pLastName)
                cn.Open()
                Dim reader As OleDbDataReader = cmd.ExecuteReader
                If reader.HasRows Then
                    While reader.Read
                        Console.WriteLine(reader.GetInt32(0))
                    End While
                End If
            End Using
        End Using
    End Sub

    Of course we need to make this work in real life so the first code block changes to this.

    Dim ops As New Operations
    ops.FindExample($"%{txtFirstNameFilter.Text}%", $"%{txtLastNameFilter.Text}%")

    You could do the % signs in the find code also.

    Results

    1
    2
    3
    4
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Sunday, September 30, 2018 7:48 PM
    Sunday, September 30, 2018 7:21 PM
    Moderator