Publish date: 2003-04-23

Filter in Data Grid (VB.Net)

Download the example archive
 Imports System.Data.OleDb

Public Class TDataSetObject

    'OleDb Variables
    Public mConnection As OleDbConnection   'OleDbConnection
    Public mSelectCmd As OleDbCommand       'Select Command
    Public mDataAdapter As OleDbDataAdapter 'Data Adapter - for filling DataSet 

    Public mDataSet As DataSet 'Data Source for DataGrid

    Public Sub New()
        'create new connection from udl file
        mConnection = New OleDbConnection("file name=employee.udl")

        'select command
        mSelectCmd = New OleDbCommand("select * from employee", _
                                       mConnection)
        'configuring Data Adapter
        mDataAdapter = New OleDbDataAdapter(mSelectCmd)

    End Sub

    'fill DataSet
    Public Sub FillDataSet()
        Try
            mDataSet = New DataSet("Employers Listing Example")

            Dim myTrans As OleDbTransaction
            'block for filling DataGrid
            mConnection.Open()
            myTrans = mConnection.BeginTransaction()
            mSelectCmd.Transaction = myTrans

            mDataAdapter.Fill(mDataSet, "Employers")

            myTrans.Commit()
            mConnection.Close()
            'end block for filling DataSet

        Catch myException As OleDbException
            Dim str As String, i As Integer
            For i = 0 To myException.Errors.Count - 1
                str = str + "Message " & i.ToString + 1 & ": " + myException.Errors(i).Message + Chr(13) + _
                           "Native: " + myException.Errors(i).NativeError.ToString() + Chr(13) + _
                           "Source: " + myException.Errors(i).Source + Chr(13) + _
                           "SQL: " + myException.Errors(i).SQLState + Chr(13) + _
                           "----------------------------------------" + Chr(13)
            Next i
            MsgBox(str)
        Catch e As Exception
            MsgBox(e.Message)
        End Try
    End Sub

    'Records filters (Get The Filter String, return DataView)
    Public Function ApplyFilter(ByVal FilterString As String) As DataView
        If Not mDataSet Is Nothing Then
            ApplyFilter = New DataView(mDataSet.Tables("Employers"))
            Try
                ApplyFilter.RowFilter = FilterString
            Catch e As Exception
                MsgBox(e.Message)
            End Try
        End If
    End Function

End Class
Download the example archive