Results 1 to 4 of 4

Thread: Filtering a datagridview based on a textbox input

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    Filtering a datagridview based on a textbox input

    Using VS2017 I have created a windows form containing a datagridview populated from an Excel workbook.
    The population is performed using the following code.

    Code:
    Imports System.IO
    Imports ExcelDataReader
    Public Class Products
        Dim FilePath As String
        Dim tables As DataTableCollection
        Dim result As New DataSet
        Dim dt As DataTable
        Private Sub Products_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Using ofd As OpenFileDialog = New OpenFileDialog()
                ofd.FileName = "E:\MWP\Databases\NoDupeLinkDB.xlsx"
                tbFile.Text = ofd.FileName
                Using stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read)
                    Using reader As IExcelDataReader = ExcelReaderFactory.CreateReader(stream)
                        result = reader.AsDataSet(New ExcelDataSetConfiguration() With {
                                                                       .ConfigureDataTable = Function(__) New ExcelDataTableConfiguration() With {
                                                                       .UseHeaderRow = True}})
                        tables = result.Tables
                        cboSheet.Items.Clear()
                        For Each table As DataTable In tables
                            cboSheet.Items.Add(table.TableName)
                        Next
                    End Using
                End Using
            End Using
            cboSheet.Text = "Tabs"
            dt = tables(cboSheet.SelectedItem.ToString())
    
            dgvProdND.DataSource = dt
    
        End Sub
    This works fine.
    What I am looking to do is to type something into a textbox and have a second datagrid populated with matching records from the first.
    So I would have dgvProdND populated as above; type something like "Abil" into the textbox and have a second datagrid (dgvResult) populated with rows from dgvProdND where the "Description" column matches, or is like the typed text.
    Research on this appears to always refer to querying the source database (usually MS Access); but I don't have a connection to an Access database.
    I'm fairly new to vb so please be gentle!!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Filtering a datagridview based on a textbox input

    A database is irrelevant. Bind your DataTable to a BindingSource and bind that to the grid, then set the Filter property of the BindingSource.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    Re: Filtering a datagridview based on a textbox input

    Quote Originally Posted by jmcilhinney View Post
    A database is irrelevant. Bind your DataTable to a BindingSource and bind that to the grid, then set the Filter property of the BindingSource.
    Thanks for the reply. As I said, I'm fairly new to vb. Could you possibly guide me through the binding process?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Filtering a datagridview based on a textbox input

    You already know how to do it because you're already doing it. You're binding the DataTable to grid directly. Just put a BindingSource, which you would add in the designer, in between. It's two steps instead of one but its still the same steps. Apart from that, you could find examples on the web easily enough if you looked.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width