-
Sep 13th, 2020, 12:01 PM
#1
Thread Starter
Lively Member
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!!!
-
Sep 13th, 2020, 01:02 PM
#2
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.
-
Sep 13th, 2020, 04:26 PM
#3
Thread Starter
Lively Member
Re: Filtering a datagridview based on a textbox input
Originally Posted by jmcilhinney
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?
-
Sep 13th, 2020, 08:07 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|