filter DataGridView

  • 12 years ago

     I am currently running VS2008 and this is the following code I use to populate 2 DataGridView's on my application......

    Imports System.Data
    Imports System.Data.OleDb
    
    Public Class Form1
        Dim strTestConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\Game.mdb"
        Dim strSQLSelect1 As String = "SELECT * FROM Armor"
        Dim strSQLSelect2 As String = "SELECT * FROM Player"
        Dim ArmorDataAdapter As New OleDbDataAdapter(strSQLSelect1, strTestConnection)
        Dim PlayerDataAdapter As New OleDbDataAdapter(strSQLSelect2, strTestConnection)
        Dim TestDataSet As New DataSet()
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ArmorDataAdapter.Fill(TestDataSet, "Armor")
            PlayerDataAdapter.Fill(TestDataSet, "Player")
            DataGridView1.DataSource = TestDataSet
            DataGridView1.DataMember = "Armor"
            DataGridView2.DataSource = TestDataSet
            DataGridView2.DataMember = "Player"
        End Sub
    End Class

     My question is this... I want to add either a textbox and use its textchanged event?

    OR Add a textbox and a button, when the user clicks the button the code I want to run will run....

    I want to be able to use this textbox to sort through the records in the DGV ....

    FOR EXAMPLE  If my stored records are JAMES, JASON, JACK

    A user types "JA" into the textbox and all the records stay because all the names start with JA

    If they go one step further and type "JAM" into the textbox, only JAMES' record will show in the DGV and the rest will be invisible.

    when the textbox = Nothing ....... all records return......  How do I do this?

    I have seen filters like this created in previous versions, just not sure how to update them.

    NOTE: Although the above code populates 2 DGV's, the filter itself, need only apply to one as there will only be one DGV, I was just messing around with a tutorial on how to load 2 DGV's from one dataset I did search the internet for the answer to this, but none quite gave me the answer I was looking for.

  • 12 years ago

    use datatable instead of dataset, there you can add filter to it which is immediately through datagrid/view.
    for show all you only set filter to empty text, and for JOH found JOHN use "column LIKE "%JOH%"

    you can look at my Support.lDataTable.SearchDT function for inspiration

  • 12 years ago

    Just to slightly extend upon konikulas idea of using the DataTable.Select method, perhaps another possibility is to use a dataview?

    http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx

     

  • 12 years ago

    thank you both for the suggestions ....

    I will read up on both and reply back if I was able to solve it :)

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“XML is like violence - if it's not working for you, you're not using enough of it.”