none
filter datagridview using id no. RRS feed

Answers

  • You really should use a parameter even if there is a remote chance of an apostrophe in the value e.g. if we look at an example behind the code how this would play out (an exception).

    While the right way (this is done in a editor) is to have one MySql parameter for the WHERE condition.



    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


    Tuesday, September 11, 2018 11:20 AM
    Moderator

All replies

  • If the data are got from a DataTable using “binding”, then try something like this:

       myDataTable1.DefaultView.RowFilter = "Id=1234"

    Otherwise, give details about your rows.


    Sunday, September 9, 2018 7:36 AM
  • Hello,

    Without specific details we truly can only guess at what you are after. 

    Generally speaking, typically using a "id" to filter seems like a range would be the case e.g.

    ID >= 12 AND ID <= 20

    And then there is "Find" e.g.

    ID = 12

    No matter, when there the DataGridView is loaded by setting the DataSource using a DataTable Viorel_ suggestion will work for you for an exact match.

    Now if by chance you were using a TableAdapter to populate the DataGridView there is a BindingSource which makes things easy for a filter e.g.

    SomeBindingSource.Filter = "ID >= 12 AND ID <= 20"

    Dynamic filter

    Dim startId As Integer = 12
    Dim endId As Integer = 20
    SomeBindingSource.Filter = $"ID >= {startId} AND ID <= {endId}"

    Remove 

    SomeBindingSource.Filter = ""

    Then there is "Find" using TextBox input

    Dim id As Integer = 0
    If Integer.TryParse(txtIdentifier.Text, id) Then
        Dim pos As Integer = SomeBindingSource.Find("ID", id)
        If pos > -1 Then
            SomeBindingSource.Position = pos
        End If
    End If
    

    Of course none of the above is indicating a solution but showing you a possibility and may be way off base as you have not supplied details.



    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

    Sunday, September 9, 2018 8:57 AM
    Moderator
  • I just want to filter my datagridview by searching id no. (integer) but only works with string

    this is my code...

    con = New MySqlConnection
            con.ConnectionString = str
            Dim sda As New MySqlDataAdapter
            Dim bsource As New BindingSource
            Try
                con.Open()
                Dim query1 As String = "select * from conice.diagnose"
                command = New MySqlCommand(query1, con)
                sda.SelectCommand = command
                sda.Fill(dbdataset3)
                bsource.DataSource = dbdataset3
                dgvdental.DataSource = bsource
                sda.Update(dbdataset3)

                con.Close()
            Catch ex As Exception
            End Try

    .....

    Dim k As New DataView(dbdataset3)
            k.RowFilter = String.Format("[idno.] like '%{0}%'", txtboxdental.Text)
            dgvdental.DataSource = k

    Tuesday, September 11, 2018 10:48 AM
  • For instance, if I want to filter service and  I will do Like '%" & txtboxsearch.Text & "%'   - the service's datatype is varchar
    My question is, what should I write for the "int" datatype

    Dim adapater As MySqlDataAdapter
            Dim ds As New DataSet
            Try
                con.Open()
                adapater = New MySqlDataAdapter("Select * from schedule where service Like '%" & txtboxsearch.Text & "%' ", con)
                adapater.Fill(ds)
                DataGridView1.DataSource = ds.Tables(0)
                con.Close()
            Catch ex As Exception
            End Try

    Tuesday, September 11, 2018 10:55 AM
  • You really should use a parameter even if there is a remote chance of an apostrophe in the value e.g. if we look at an example behind the code how this would play out (an exception).

    While the right way (this is done in a editor) is to have one MySql parameter for the WHERE condition.



    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


    Tuesday, September 11, 2018 11:20 AM
    Moderator
  • For instance, if I want to filter service and  I will do Like '%" & txtboxsearch.Text & "%'   - the service's datatype is varchar
    My question is, what should I write for the "int" datatype

    Dim adapater As MySqlDataAdapter
            Dim ds As New DataSet
            Try
                con.Open()
                adapater = New MySqlDataAdapter("Select * from schedule where service Like '%" & txtboxsearch.Text & "%' ", con)
                adapater.Fill(ds)
                DataGridView1.DataSource = ds.Tables(0)
                con.Close()
            Catch ex As Exception
            End Try

    See the following which suggest casting or using a range.

    https://www.ryadel.com/en/like-operator-equivalent-integer-numeric-columns-sql-t-sql-database/


    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

    Tuesday, September 11, 2018 3:24 PM
    Moderator