Results 1 to 5 of 5

Thread: [RESOLVED] Datagrid Search Data

  1. #1

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Resolved [RESOLVED] Datagrid Search Data

    i have code to load data from database to DataGridView.
    VB.NET Code:
    1. Imports System.Data
    2. Imports System.Data.OleDb
    3.  
    4. Public Class Form1
    5.  
    6.     Dim dt As New DataTable
    7.  
    8.  
    9.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    10.         Me.WindowState = FormWindowState.Maximized
    11.     End Sub
    12.  
    13.     Sub setData()
    14.         Try
    15.             Dim scmd As New OleDbCommand
    16.  
    17.             scmd.CommandText = "select prod_id,prod_name,unit,unitprice,unitinstock from inv_t_products order by prod_id"
    18.             scmd.Connection = dbConn
    19.  
    20.             Dim da As New OleDbDataAdapter
    21.             da.SelectCommand = scmd
    22.             da.Fill(dt)
    23.  
    24.             dg.DataSource = dt
    25.  
    26.         Catch ex As Exception
    27.  
    28.         End Try
    29.  
    30.  
    31.     End Sub
    32.  
    33.     Private Sub cmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRefresh.Click
    34.         setData()
    35.     End Sub
    36.  
    37.     Private Sub tFilter_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tFilter.TextChanged
    38.         ' search data
    39.         ' ???
    40.  
    41.  
    42.     End Sub
    43. End Class

    at event
    VB.NET Code:
    1. Private Sub tFilter_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tFilter.TextChanged
    2.         ' search data
    3.         ' ???
    4.  
    5.  
    6.     End Sub


    I Want to find data by prod_name field, when someone fill data to tFilter (textbox).

  2. #2

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Datagrid Search Data

    i try like this :
    VB.NET Code:
    1. Private Sub tFilter_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tFilter.TextChanged
    2.         ' search data
    3.         ' ???
    4.  
    5.         Dim dv As New DataView(dt.DataSet.Tables(0))
    6.         dv.RowFilter = "prod_name like '%" & Trim(tFilter.Text) & "%' "
    7.         dg.DataSource = dv
    8.     End Sub

    get error :
    Code:
    Object reference not set to an instance of an object.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagrid Search Data

    The specific cause of your exception is the fact that you're trying to get the Tables property of a DataSet that doesn't exist. DataTables are not inherently part of a DataSet. You've never added your DataTable to a DataSet so its DataSet property is Nothing.

    That said, what's the point of getting the DataSet even if their was one? This code:
    Code:
    dt.DataSet.Tables(0)
    is like me saying get my father, then get his sons and then get the first one of them. That's me! I'm back where I started, so what was the point of going anywhere? That code would be functionally equivalent to just:
    Code:
    dt
    Furthermore, you shouldn't be creating a new and rebinding anyway. If you wanted to bind to a DataView then you should be creating it in the first place and binding it once and once only. You then change the RowFilter of that existing DataView. You don't have to do that though, because every DataTable is associated with a DataView by default and, when you bind, it's from that DataView that the data comes any way. That's how you're able to sort a grid that's bound to a DataTable. You should replace all that code in the TextChanged event handler with just this:
    vb.net Code:
    1. dt.DefaultView.RowFilter = String.Format("prod_name LIKE '%{0}%'", tFilter.Text.Trim())
    Having said that, is it really a good idea to filter the data after every text change? What if the user wants to type in 10 characters? You'll end up filtering the data 10 times when you only need to do it once. It's a good idea to use a Timer to add a delay between the text change and the event that you can restart each time another character is entered. That way, unless the user types very slowly, the data will only be filtered once when they type multiple characters. You can find an example I provided recently here:

    http://www.vbforums.com/showthread.p...ighlight=Timer
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Datagrid Search Data

    thank you, I have tried and succeeded.

    continue whether the data shown is the data currently existing in the database? My intention is always connected with a database datagrid

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagrid Search Data

    Quote Originally Posted by Tengkorak View Post
    thank you, I have tried and succeeded.

    continue whether the data shown is the data currently existing in the database? My intention is always connected with a database datagrid
    That's got nothing to do with the subject of this thread, which is how to filter data. Please keep each thread to a single topic and each topic to a single thread.

    ADO.NET is, by design, disconnected. The data you have represents a snalshot of the database at the time you retrieved it. If you want your app to reflect changes made to the data by other clients then you would have to requery the database.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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