vb.netvb.net-2010

Is there a way to jump to record I have searched for and still display values less and greater than the value I searched for?


Please assist. Instead of filtering I want to find a value for example value 10 and jump to that value 10 but still display values that are less than 10 and greater than 10. This value is a size of something so if I don't have size 10 I want to be able to see available sizes greater or less as they may suite my need. Values are sorted in ascending order So instead of filtering the data I am jumping to value 10 and should be able to scroll up or down to see values less or greater than my value. If The value 10 is not available it can jump to the next greater value. My code below is not selecting the value I searched for, it is just displaying from zero.

Sub search()
    Try
        DataGridRecords.Rows.Clear()
        conn.Open()

        Dim cmd As New OleDb.OleDbCommand("Select * from tblDies where  `IDSIZE` <> " & txtIdSize.Text & " ", conn)

        dr = cmd.ExecuteReader
        While dr.Read
            DataGridRecords.Rows.Add(dr.Item("ID"), dr.Item("DIENUMBER"), dr.Item("DESCRIPT"), dr.Item("OLDNUMBER"), dr.Item("CODE"), dr.Item("QUANTITY"), dr.Item("IDSIZE"), dr.Item("ODSIZE"), dr.Item("HEIGHT"), dr.Item("FLANGE HT"), dr.Item("FLANGE DIA"), dr.Item("CuRef"), dr.Item("CUTEL"), dr.Item("CuContact"), dr.Item("Price-selling"), dr.Item("P/no"), dr.Item("Stoksize"), dr.Item("Material"), dr.Item("Shore"), dr.Item("DieChkd"), dr.Item("DIECOST"), dr.Item("DATE"), dr.Item("REMARKS"), dr.Item("PRREF"))
        End While
        DataGridRecords.Sort(DataGridRecords.Columns(6), System.ComponentModel.ListSortDirection.Ascending)
        dr.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    conn.Close()
End Sub

Solution

  • Don't add the data directly to your grid. Populate a DataTable and bind that to the grid via a BindingSource. If you need to sort (you should use the query by default) then set the Sort property. The BindingSource has a Find method that will return an index, which you can assign to its Position property.

    Dim table As New DataTable
    
    table.Load(myDataReader)
    
    myBindingSource.DataSource = table
    myDataGridView.DataSource = myBindingSource
    
    myBindingSource.Position = myBindingSource.Find(columnName, value)