|
-
Mar 15th, 2011, 11:37 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Datagrid Search Data
i have code to load data from database to DataGridView.
VB.NET Code:
Imports System.Data Imports System.Data.OleDb Public Class Form1 Dim dt As New DataTable Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.WindowState = FormWindowState.Maximized End Sub Sub setData() Try Dim scmd As New OleDbCommand scmd.CommandText = "select prod_id,prod_name,unit,unitprice,unitinstock from inv_t_products order by prod_id" scmd.Connection = dbConn Dim da As New OleDbDataAdapter da.SelectCommand = scmd da.Fill(dt) dg.DataSource = dt Catch ex As Exception End Try End Sub Private Sub cmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRefresh.Click setData() End Sub Private Sub tFilter_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tFilter.TextChanged ' search data ' ??? End Sub End Class
at event
VB.NET Code:
Private Sub tFilter_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tFilter.TextChanged ' search data ' ??? End Sub
I Want to find data by prod_name field, when someone fill data to tFilter (textbox).
-
Mar 15th, 2011, 11:54 PM
#2
Thread Starter
Addicted Member
Re: Datagrid Search Data
i try like this :
VB.NET Code:
Private Sub tFilter_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tFilter.TextChanged
' search data
' ???
Dim dv As New DataView(dt.DataSet.Tables(0))
dv.RowFilter = "prod_name like '%" & Trim(tFilter.Text) & "%' "
dg.DataSource = dv
End Sub
get error :
Code:
Object reference not set to an instance of an object.
-
Mar 16th, 2011, 12:53 AM
#3
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: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:
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
-
Mar 16th, 2011, 01:17 AM
#4
Thread Starter
Addicted Member
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
-
Mar 16th, 2011, 01:21 AM
#5
Re: Datagrid Search Data
 Originally Posted by Tengkorak
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.
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
|