Simple Example of Using a DataView
How do I display a filtered list in a DataGrid based on text the user types into a Textbox?
To set this up you will need a database, a form with a Datagrid and a Textbox. You'll need to change the names of things to fit your situation.
The end result is for the Datagrid to display the data as a filtered view based on what the user is typing into the textbox. In this example, if the user types "a", it should show all names that start with "a" or "A". If they type "An", the grid should display all names that start with "An" (case insensetive)... and so on.
Private _data As DataTable
Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged
' NOTE: Add some exception handling
Dim dv As New DataView
If _data Is Nothing Then
InitializeData()
End If
dv.Table = _data
dv.RowFilter = "FirstName LIKE '" & txtName.Text & "%'"
noteGrid.DataSource = dv
End Sub
Private Sub InitializeData()
' NOTE: Add some exception handling
Dim cn As OleDbConnection
Dim s As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.AppDomain.CurrentDomain.BaseDirectory() & "MyData.mdb;"
cn = New OleDbConnection(s)
cn.Open()
Dim da As New OleDbDataAdapter("Select FirstName from People ORDER BY FirstName", cn)
_data = New DataTable
da.Fill(_data)
_data.TableName = "People"
End Sub
