Imports System.Data.OleDb
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents tabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents tabPageSource As System.Windows.Forms.TabPage
    Friend WithEvents label7 As System.Windows.Forms.Label
    Friend WithEvents comboTables As System.Windows.Forms.ComboBox
    Friend WithEvents label6 As System.Windows.Forms.Label
    Friend WithEvents tabPage2 As System.Windows.Forms.TabPage
    Friend WithEvents groupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents comboValue As System.Windows.Forms.ComboBox
    Friend WithEvents label14 As System.Windows.Forms.Label
    Friend WithEvents comboOperator As System.Windows.Forms.ComboBox
    Friend WithEvents label13 As System.Windows.Forms.Label
    Friend WithEvents buttonAddFilter As System.Windows.Forms.Button
    Friend WithEvents comboFields2 As System.Windows.Forms.ComboBox
    Friend WithEvents label12 As System.Windows.Forms.Label
    Friend WithEvents buttonRemoveFilter As System.Windows.Forms.Button
    Friend WithEvents textFilter As System.Windows.Forms.TextBox
    Friend WithEvents label10 As System.Windows.Forms.Label
    Friend WithEvents listFilters As System.Windows.Forms.ListBox
    Friend WithEvents label9 As System.Windows.Forms.Label
    Friend WithEvents tabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents buttonRemove As System.Windows.Forms.Button
    Friend WithEvents buttonDown As System.Windows.Forms.Button
    Friend WithEvents buttonUp As System.Windows.Forms.Button
    Friend WithEvents groupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents buttonAddSort As System.Windows.Forms.Button
    Friend WithEvents label5 As System.Windows.Forms.Label
    Friend WithEvents comboSortMode As System.Windows.Forms.ComboBox
    Friend WithEvents comboFields As System.Windows.Forms.ComboBox
    Friend WithEvents label4 As System.Windows.Forms.Label
    Friend WithEvents textSort As System.Windows.Forms.TextBox
    Friend WithEvents label3 As System.Windows.Forms.Label
    Friend WithEvents label2 As System.Windows.Forms.Label
    Friend WithEvents listSorts As System.Windows.Forms.ListBox
    Friend WithEvents tabPage3 As System.Windows.Forms.TabPage
    Friend WithEvents dataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents c1DataSet1 As C1.Data.C1DataSet
    Friend WithEvents c1SchemaDef1 As C1.Data.C1SchemaDef
    Friend WithEvents c1DataView1 As C1.Data.C1DataView
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.tabControl1 = New System.Windows.Forms.TabControl
        Me.tabPageSource = New System.Windows.Forms.TabPage
        Me.label7 = New System.Windows.Forms.Label
        Me.comboTables = New System.Windows.Forms.ComboBox
        Me.label6 = New System.Windows.Forms.Label
        Me.tabPage2 = New System.Windows.Forms.TabPage
        Me.groupBox2 = New System.Windows.Forms.GroupBox
        Me.comboValue = New System.Windows.Forms.ComboBox
        Me.label14 = New System.Windows.Forms.Label
        Me.comboOperator = New System.Windows.Forms.ComboBox
        Me.label13 = New System.Windows.Forms.Label
        Me.buttonAddFilter = New System.Windows.Forms.Button
        Me.comboFields2 = New System.Windows.Forms.ComboBox
        Me.label12 = New System.Windows.Forms.Label
        Me.buttonRemoveFilter = New System.Windows.Forms.Button
        Me.textFilter = New System.Windows.Forms.TextBox
        Me.label10 = New System.Windows.Forms.Label
        Me.listFilters = New System.Windows.Forms.ListBox
        Me.label9 = New System.Windows.Forms.Label
        Me.tabPage1 = New System.Windows.Forms.TabPage
        Me.buttonRemove = New System.Windows.Forms.Button
        Me.buttonDown = New System.Windows.Forms.Button
        Me.buttonUp = New System.Windows.Forms.Button
        Me.groupBox1 = New System.Windows.Forms.GroupBox
        Me.buttonAddSort = New System.Windows.Forms.Button
        Me.label5 = New System.Windows.Forms.Label
        Me.comboSortMode = New System.Windows.Forms.ComboBox
        Me.comboFields = New System.Windows.Forms.ComboBox
        Me.label4 = New System.Windows.Forms.Label
        Me.textSort = New System.Windows.Forms.TextBox
        Me.label3 = New System.Windows.Forms.Label
        Me.label2 = New System.Windows.Forms.Label
        Me.listSorts = New System.Windows.Forms.ListBox
        Me.tabPage3 = New System.Windows.Forms.TabPage
        Me.dataGrid1 = New System.Windows.Forms.DataGrid
        Me.c1DataSet1 = New C1.Data.C1DataSet
        Me.c1SchemaDef1 = New C1.Data.C1SchemaDef
        Me.c1DataView1 = New C1.Data.C1DataView
        Me.tabControl1.SuspendLayout()
        Me.tabPageSource.SuspendLayout()
        Me.tabPage2.SuspendLayout()
        Me.groupBox2.SuspendLayout()
        Me.tabPage1.SuspendLayout()
        Me.groupBox1.SuspendLayout()
        Me.tabPage3.SuspendLayout()
        CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.c1DataSet1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.c1SchemaDef1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.c1DataView1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'tabControl1
        '
        Me.tabControl1.Controls.Add(Me.tabPageSource)
        Me.tabControl1.Controls.Add(Me.tabPage2)
        Me.tabControl1.Controls.Add(Me.tabPage1)
        Me.tabControl1.Controls.Add(Me.tabPage3)
        Me.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.tabControl1.Location = New System.Drawing.Point(0, 0)
        Me.tabControl1.Name = "tabControl1"
        Me.tabControl1.SelectedIndex = 0
        Me.tabControl1.Size = New System.Drawing.Size(544, 365)
        Me.tabControl1.TabIndex = 1
        '
        'tabPageSource
        '
        Me.tabPageSource.Controls.Add(Me.label7)
        Me.tabPageSource.Controls.Add(Me.comboTables)
        Me.tabPageSource.Controls.Add(Me.label6)
        Me.tabPageSource.Location = New System.Drawing.Point(4, 22)
        Me.tabPageSource.Name = "tabPageSource"
        Me.tabPageSource.Size = New System.Drawing.Size(536, 339)
        Me.tabPageSource.TabIndex = 3
        Me.tabPageSource.Text = "Select DataSource"
        '
        'label7
        '
        Me.label7.Location = New System.Drawing.Point(8, 72)
        Me.label7.Name = "label7"
        Me.label7.Size = New System.Drawing.Size(100, 16)
        Me.label7.TabIndex = 3
        Me.label7.Text = "Table view name:"
        '
        'comboTables
        '
        Me.comboTables.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.comboTables.Location = New System.Drawing.Point(8, 88)
        Me.comboTables.Name = "comboTables"
        Me.comboTables.Size = New System.Drawing.Size(232, 21)
        Me.comboTables.TabIndex = 2
        '
        'label6
        '
        Me.label6.Location = New System.Drawing.Point(8, 24)
        Me.label6.Name = "label6"
        Me.label6.Size = New System.Drawing.Size(328, 32)
        Me.label6.TabIndex = 1
        Me.label6.Text = "Please select a table view from the list below. Then create sort/filter condition" & _
            "s on Sorting/Filtering tabs."
        '
        'tabPage2
        '
        Me.tabPage2.Controls.Add(Me.groupBox2)
        Me.tabPage2.Controls.Add(Me.buttonRemoveFilter)
        Me.tabPage2.Controls.Add(Me.textFilter)
        Me.tabPage2.Controls.Add(Me.label10)
        Me.tabPage2.Controls.Add(Me.listFilters)
        Me.tabPage2.Controls.Add(Me.label9)
        Me.tabPage2.Location = New System.Drawing.Point(4, 22)
        Me.tabPage2.Name = "tabPage2"
        Me.tabPage2.Size = New System.Drawing.Size(536, 339)
        Me.tabPage2.TabIndex = 1
        Me.tabPage2.Text = "Filtering"
        Me.tabPage2.Visible = False
        '
        'groupBox2
        '
        Me.groupBox2.Controls.Add(Me.comboValue)
        Me.groupBox2.Controls.Add(Me.label14)
        Me.groupBox2.Controls.Add(Me.comboOperator)
        Me.groupBox2.Controls.Add(Me.label13)
        Me.groupBox2.Controls.Add(Me.buttonAddFilter)
        Me.groupBox2.Controls.Add(Me.comboFields2)
        Me.groupBox2.Controls.Add(Me.label12)
        Me.groupBox2.Location = New System.Drawing.Point(240, 40)
        Me.groupBox2.Name = "groupBox2"
        Me.groupBox2.Size = New System.Drawing.Size(232, 168)
        Me.groupBox2.TabIndex = 13
        Me.groupBox2.TabStop = False
        Me.groupBox2.Text = "New filter"
        '
        'comboValue
        '
        Me.comboValue.Location = New System.Drawing.Point(80, 88)
        Me.comboValue.Name = "comboValue"
        Me.comboValue.Size = New System.Drawing.Size(136, 21)
        Me.comboValue.TabIndex = 6
        '
        'label14
        '
        Me.label14.Location = New System.Drawing.Point(8, 88)
        Me.label14.Name = "label14"
        Me.label14.Size = New System.Drawing.Size(56, 23)
        Me.label14.TabIndex = 5
        Me.label14.Text = "Value:"
        '
        'comboOperator
        '
        Me.comboOperator.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.comboOperator.Items.AddRange(New Object() {"= (equals to)", "> (greater than)", "< (less than)", "<> (not equal to)"})
        Me.comboOperator.Location = New System.Drawing.Point(80, 56)
        Me.comboOperator.Name = "comboOperator"
        Me.comboOperator.Size = New System.Drawing.Size(136, 21)
        Me.comboOperator.TabIndex = 4
        '
        'label13
        '
        Me.label13.Location = New System.Drawing.Point(8, 56)
        Me.label13.Name = "label13"
        Me.label13.Size = New System.Drawing.Size(56, 16)
        Me.label13.TabIndex = 3
        Me.label13.Text = "Operator:"
        '
        'buttonAddFilter
        '
        Me.buttonAddFilter.Location = New System.Drawing.Point(136, 128)
        Me.buttonAddFilter.Name = "buttonAddFilter"
        Me.buttonAddFilter.Size = New System.Drawing.Size(75, 23)
        Me.buttonAddFilter.TabIndex = 2
        Me.buttonAddFilter.Text = "Add"
        '
        'comboFields2
        '
        Me.comboFields2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.comboFields2.Location = New System.Drawing.Point(56, 24)
        Me.comboFields2.Name = "comboFields2"
        Me.comboFields2.Size = New System.Drawing.Size(160, 21)
        Me.comboFields2.TabIndex = 1
        '
        'label12
        '
        Me.label12.Location = New System.Drawing.Point(8, 24)
        Me.label12.Name = "label12"
        Me.label12.Size = New System.Drawing.Size(32, 23)
        Me.label12.TabIndex = 0
        Me.label12.Text = "Field"
        '
        'buttonRemoveFilter
        '
        Me.buttonRemoveFilter.Location = New System.Drawing.Point(168, 24)
        Me.buttonRemoveFilter.Name = "buttonRemoveFilter"
        Me.buttonRemoveFilter.Size = New System.Drawing.Size(56, 23)
        Me.buttonRemoveFilter.TabIndex = 4
        Me.buttonRemoveFilter.Text = "Remove"
        '
        'textFilter
        '
        Me.textFilter.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.textFilter.Location = New System.Drawing.Point(8, 264)
        Me.textFilter.Name = "textFilter"
        Me.textFilter.ReadOnly = True
        Me.textFilter.Size = New System.Drawing.Size(520, 20)
        Me.textFilter.TabIndex = 3
        '
        'label10
        '
        Me.label10.Location = New System.Drawing.Point(8, 240)
        Me.label10.Name = "label10"
        Me.label10.Size = New System.Drawing.Size(384, 16)
        Me.label10.TabIndex = 2
        Me.label10.Text = "RowFilter property value (this string is assigned to dataView1.RowFilter property" & _
            "):"
        '
        'listFilters
        '
        Me.listFilters.Location = New System.Drawing.Point(8, 24)
        Me.listFilters.Name = "listFilters"
        Me.listFilters.Size = New System.Drawing.Size(152, 212)
        Me.listFilters.TabIndex = 1
        '
        'label9
        '
        Me.label9.Location = New System.Drawing.Point(8, 8)
        Me.label9.Name = "label9"
        Me.label9.Size = New System.Drawing.Size(112, 16)
        Me.label9.TabIndex = 0
        Me.label9.Text = "Applied filters:"
        '
        'tabPage1
        '
        Me.tabPage1.Controls.Add(Me.buttonRemove)
        Me.tabPage1.Controls.Add(Me.buttonDown)
        Me.tabPage1.Controls.Add(Me.buttonUp)
        Me.tabPage1.Controls.Add(Me.groupBox1)
        Me.tabPage1.Controls.Add(Me.textSort)
        Me.tabPage1.Controls.Add(Me.label3)
        Me.tabPage1.Controls.Add(Me.label2)
        Me.tabPage1.Controls.Add(Me.listSorts)
        Me.tabPage1.Location = New System.Drawing.Point(4, 22)
        Me.tabPage1.Name = "tabPage1"
        Me.tabPage1.Size = New System.Drawing.Size(536, 339)
        Me.tabPage1.TabIndex = 0
        Me.tabPage1.Text = "Sorting"
        Me.tabPage1.Visible = False
        '
        'buttonRemove
        '
        Me.buttonRemove.Location = New System.Drawing.Point(168, 24)
        Me.buttonRemove.Name = "buttonRemove"
        Me.buttonRemove.Size = New System.Drawing.Size(56, 23)
        Me.buttonRemove.TabIndex = 8
        Me.buttonRemove.Text = "Remove"
        '
        'buttonDown
        '
        Me.buttonDown.Location = New System.Drawing.Point(168, 88)
        Me.buttonDown.Name = "buttonDown"
        Me.buttonDown.Size = New System.Drawing.Size(56, 24)
        Me.buttonDown.TabIndex = 7
        Me.buttonDown.Text = "Down"
        '
        'buttonUp
        '
        Me.buttonUp.Location = New System.Drawing.Point(168, 56)
        Me.buttonUp.Name = "buttonUp"
        Me.buttonUp.Size = New System.Drawing.Size(56, 24)
        Me.buttonUp.TabIndex = 6
        Me.buttonUp.Text = "Up"
        '
        'groupBox1
        '
        Me.groupBox1.Controls.Add(Me.buttonAddSort)
        Me.groupBox1.Controls.Add(Me.label5)
        Me.groupBox1.Controls.Add(Me.comboSortMode)
        Me.groupBox1.Controls.Add(Me.comboFields)
        Me.groupBox1.Controls.Add(Me.label4)
        Me.groupBox1.Location = New System.Drawing.Point(232, 40)
        Me.groupBox1.Name = "groupBox1"
        Me.groupBox1.Size = New System.Drawing.Size(240, 136)
        Me.groupBox1.TabIndex = 5
        Me.groupBox1.TabStop = False
        Me.groupBox1.Text = "New sort field"
        '
        'buttonAddSort
        '
        Me.buttonAddSort.Location = New System.Drawing.Point(144, 88)
        Me.buttonAddSort.Name = "buttonAddSort"
        Me.buttonAddSort.Size = New System.Drawing.Size(75, 23)
        Me.buttonAddSort.TabIndex = 4
        Me.buttonAddSort.Text = "Add"
        '
        'label5
        '
        Me.label5.Location = New System.Drawing.Point(8, 56)
        Me.label5.Name = "label5"
        Me.label5.Size = New System.Drawing.Size(100, 23)
        Me.label5.TabIndex = 3
        Me.label5.Text = "Order:"
        '
        'comboSortMode
        '
        Me.comboSortMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.comboSortMode.Items.AddRange(New Object() {"ASC", "DESC"})
        Me.comboSortMode.Location = New System.Drawing.Point(144, 56)
        Me.comboSortMode.Name = "comboSortMode"
        Me.comboSortMode.Size = New System.Drawing.Size(80, 21)
        Me.comboSortMode.TabIndex = 2
        '
        'comboFields
        '
        Me.comboFields.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.comboFields.Location = New System.Drawing.Point(64, 24)
        Me.comboFields.Name = "comboFields"
        Me.comboFields.Size = New System.Drawing.Size(160, 21)
        Me.comboFields.TabIndex = 1
        '
        'label4
        '
        Me.label4.Location = New System.Drawing.Point(8, 24)
        Me.label4.Name = "label4"
        Me.label4.Size = New System.Drawing.Size(40, 16)
        Me.label4.TabIndex = 0
        Me.label4.Text = "Field:"
        '
        'textSort
        '
        Me.textSort.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.textSort.Location = New System.Drawing.Point(8, 264)
        Me.textSort.Name = "textSort"
        Me.textSort.ReadOnly = True
        Me.textSort.Size = New System.Drawing.Size(520, 20)
        Me.textSort.TabIndex = 4
        '
        'label3
        '
        Me.label3.Location = New System.Drawing.Point(8, 240)
        Me.label3.Name = "label3"
        Me.label3.Size = New System.Drawing.Size(368, 16)
        Me.label3.TabIndex = 3
        Me.label3.Text = "Sort property value (this string is assigned to dataView1.Sort property):"
        '
        'label2
        '
        Me.label2.Location = New System.Drawing.Point(8, 8)
        Me.label2.Name = "label2"
        Me.label2.Size = New System.Drawing.Size(168, 16)
        Me.label2.TabIndex = 2
        Me.label2.Text = "Applied sort criterias:"
        '
        'listSorts
        '
        Me.listSorts.Location = New System.Drawing.Point(8, 24)
        Me.listSorts.Name = "listSorts"
        Me.listSorts.Size = New System.Drawing.Size(152, 212)
        Me.listSorts.TabIndex = 1
        '
        'tabPage3
        '
        Me.tabPage3.Controls.Add(Me.dataGrid1)
        Me.tabPage3.Location = New System.Drawing.Point(4, 22)
        Me.tabPage3.Name = "tabPage3"
        Me.tabPage3.Size = New System.Drawing.Size(536, 339)
        Me.tabPage3.TabIndex = 2
        Me.tabPage3.Text = "Resultset"
        Me.tabPage3.Visible = False
        '
        'dataGrid1
        '
        Me.dataGrid1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.dataGrid1.DataMember = ""
        Me.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dataGrid1.Location = New System.Drawing.Point(8, 8)
        Me.dataGrid1.Name = "dataGrid1"
        Me.dataGrid1.Size = New System.Drawing.Size(520, 320)
        Me.dataGrid1.TabIndex = 2
        '
        'c1DataSet1
        '
        Me.c1DataSet1.DataLibrary = ""
        Me.c1DataSet1.DataLibraryUrl = ""
        Me.c1DataSet1.DataSetDef = "DataSet1"
        Me.c1DataSet1.Locale = New System.Globalization.CultureInfo("en-US")
        Me.c1DataSet1.Name = "c1DataSet1"
        Me.c1DataSet1.SchemaClassName = ""
        Me.c1DataSet1.SchemaDef = Me.c1SchemaDef1
        '
        'c1SchemaDef1
        '
        Me.c1SchemaDef1.DummyImage = CType(resources.GetObject("c1SchemaDef1.DummyImage"), System.Drawing.Bitmap)
        Me.c1SchemaDef1.SerializedSchemaXML = Me.c1SchemaDef1.GetSchemaFromResources(resources, "c1SchemaDef1", Me)
        Me.c1SchemaDef1.SerializedDesignTimeOptionsXML = Me.c1SchemaDef1.GetDesignTimeOptionsFromResources(resources, "c1SchemaDef1", Me)
        '
        'c1DataView1
        '
        Me.c1DataView1.DataSet = Me.c1DataSet1
        Me.c1DataView1.TableName = ""
        Me.c1DataView1.TableViewName = ""
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(544, 365)
        Me.Controls.Add(Me.tabControl1)
        Me.Name = "Form1"
        Me.Text = "Using C1DataView: Filtering and sorting"
        Me.tabControl1.ResumeLayout(False)
        Me.tabPageSource.ResumeLayout(False)
        Me.tabPage2.ResumeLayout(False)
        Me.tabPage2.PerformLayout()
        Me.groupBox2.ResumeLayout(False)
        Me.tabPage1.ResumeLayout(False)
        Me.tabPage1.PerformLayout()
        Me.groupBox1.ResumeLayout(False)
        Me.tabPage3.ResumeLayout(False)
        CType(Me.dataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.c1DataSet1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.c1SchemaDef1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.c1DataView1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        c1DataSet1.Fill()
        Dim i As Integer
        For i = 0 To c1DataSet1.TableViews.Count - 1
            comboTables.Items.Add(c1DataSet1.TableViews(i).SchemaTable.Name)
        Next i
    End Sub

    Private Sub comboTables_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboTables.SelectedIndexChanged
        Try
            Cursor = Cursors.WaitCursor
            listSorts.Items.Clear()
            listFilters.Items.Clear()
            c1DataView1.Sort = ""
            c1DataView1.RowFilter = ""
            c1DataView1.TableViewName = comboTables.SelectedItem.ToString()
            If Not (c1DataView1.TableView Is Nothing) Then
                comboFields.Items.Clear()
                comboFields2.Items.Clear()
                Dim i As Integer
                For i = 0 To c1DataView1.TableView.Fields.Count - 1
                    comboFields.Items.Add(c1DataView1.TableView.Fields(i).Name)
                    comboFields2.Items.Add(c1DataView1.TableView.Fields(i).Name)
                    RefreshSort()
                    RefreshFilter()
                Next i
            End If
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub buttonAddSort_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonAddSort.Click
        If comboFields.SelectedIndex <> -1 Then
            Dim text As String = comboFields.SelectedItem.ToString()
            If comboSortMode.SelectedIndex > -1 Then
                text += " " + comboSortMode.SelectedItem.ToString()
            End If
            listSorts.Items.Add(text)
            RefreshSort()
        End If
    End Sub

    Private Sub buttonUp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonUp.Click
        If listSorts.SelectedIndex > 0 Then
            Dim idx As Integer = listSorts.SelectedIndex
            Dim sort1 As String = listSorts.Items(idx).ToString()
            Dim sort2 As String = listSorts.Items(idx - 1).ToString()
            listSorts.Items(idx - 1) = sort1
            listSorts.Items(idx) = sort2
            RefreshSort()
        End If
    End Sub

    Private Sub buttonDown_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonDown.Click
        If listSorts.SelectedIndex < listSorts.Items.Count - 1 Then
            Dim idx As Integer = listSorts.SelectedIndex
            Dim sort1 As String = listSorts.Items(idx).ToString()
            Dim sort2 As String = listSorts.Items(idx + 1).ToString()
            listSorts.Items(idx + 1) = sort1
            listSorts.Items(idx) = sort2
            RefreshSort()
        End If
    End Sub

    Private Sub buttonRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonRemove.Click
        If listSorts.SelectedIndex > -1 Then
            listSorts.Items.RemoveAt(listSorts.SelectedIndex)
            RefreshSort()
        End If
    End Sub

    Private Sub RefreshSort()
        If c1DataView1.TableView Is Nothing Then Exit Sub
        textSort.Clear()
        Dim i As Integer
        For i = 0 To listSorts.Items.Count - 1
            If textSort.Text <> "" Then
                textSort.Text += ", "
            End If
            textSort.Text += listSorts.Items(i).ToString()
        Next i
        c1DataView1.Sort = textSort.Text
        ' Need to call Refresh because c1DataView1.AutoRefresh=false
        c1DataView1.Refresh()
        dataGrid1.DataSource = Nothing
        dataGrid1.DataSource = c1DataView1
    End Sub

    Private Sub RefreshFilter()
        If c1DataView1.TableView Is Nothing Then Exit Sub
        textFilter.Clear()
        Dim i As Integer
        For i = 0 To listFilters.Items.Count - 1
            If (textFilter.Text <> "") Then
                textFilter.Text += " AND "
            End If
            textFilter.Text += listFilters.Items(i).ToString()
        Next i
        c1DataView1.RowFilter = textFilter.Text
        ' Need to call Refresh because c1DataView1.AutoRefresh=false
        c1DataView1.Refresh()
        dataGrid1.DataSource = Nothing
        dataGrid1.DataSource = c1DataView1
    End Sub

    Private Sub buttonAddFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonAddFilter.Click
        If comboFields2.SelectedIndex > -1 And comboOperator.SelectedIndex > -1 And comboValue.Text <> "" Then
            Dim colName As String = comboFields2.Text
            Dim strVal As String = comboValue.Text
            Dim operatorId As Integer = comboOperator.SelectedIndex
            If c1DataView1.TableView.Fields(colName).Type Is GetType(String) Then
                Dim found As Boolean = True
                Dim start As Integer = 0
                While (found = True)
                    found = False
                    Dim idx As Integer = strVal.IndexOf("'", start)
                    If (idx > -1) Then
                        strVal = strVal.Substring(0, idx) + "''" + strVal.Substring(idx + 1)
                        start = idx + 2
                        found = True
                    End If
                End While
                strVal = "'" + strVal + "'"
            ElseIf c1DataView1.TableView.Fields(colName).Type Is GetType(DateTime) Then
                strVal = "#" + strVal + "#"
            End If
            Dim filterText As String = colName

            Select Case operatorId
                Case 0 : filterText += " = "
                Case 1 : filterText += " > "
                Case 2 : filterText += " < "
                Case 3 : filterText += " <> "
            End Select
            filterText += strVal
            listFilters.Items.Add(filterText)
            RefreshFilter()
        Else
            MessageBox.Show("Not enough information to add a filter. Specify a field, operator and value")
        End If
    End Sub

    Private Sub buttonRemoveFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonRemoveFilter.Click
        If listFilters.SelectedIndex > -1 Then
            listFilters.Items.RemoveAt(listFilters.SelectedIndex)
            RefreshFilter()
        End If
    End Sub

    Private Sub comboFields2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboFields2.SelectedIndexChanged
        RefreshLookupValues()
    End Sub

    Private Sub RefreshLookupValues()
        comboValue.Items.Clear()
        If comboFields2.SelectedIndex < 0 Then Exit Sub
        Dim colName As String = comboFields2.Items(comboFields2.SelectedIndex).ToString()
        Dim dv1 As New C1.Data.C1DataView()
        dv1.DataSet = c1DataSet1
        dv1.TableViewName = comboTables.SelectedItem.ToString()
        dv1.Sort = colName + " ASC"
        dv1.Refresh()
        Dim i As Integer
        For i = 0 To dv1.Count - 1
            Dim row As C1.Data.C1DataRow = dv1(i)
            Dim val As Object = row(colName)
            If Not (val Is Nothing) Then
                Dim strVal As String = val.ToString()
                Dim found As Boolean = False
                Dim j As Integer
                For j = 0 To comboValue.Items.Count - 1
                    If comboValue.Items(j).ToString() = strVal Then
                        found = True
                        Exit For
                    End If
                Next j
                If Not found Then
                    comboValue.Items.Add(strVal)
                End If
            End If
        Next i
    End Sub

#Region " Switch to C1NWind.mdb database "

    Private Sub C1SchemaDef1_CreateSchema(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles c1SchemaDef1.CreateSchema
        Dim dbPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + _
            "\ComponentOne Samples\Common\C1NWind.mdb"
        If File.Exists(dbPath) Then
            Dim conn As C1.Data.SchemaObjects.Connection = C1SchemaDef1.Schema.Connections(0)
            Dim csb As New OleDbConnectionStringBuilder(conn.ConnectionString)
            csb.DataSource = dbPath
            conn.ConnectionString = csb.ToString()
        End If
    End Sub

#End Region

End Class
