Imports C1.C1DataExtender
Imports System.ComponentModel
Imports System.Data.OleDb
Imports System.IO

Public Class Form1

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        c1DataViewSet1.Views(0).Definition = definitionTextBox.Text
        RefreshSortFilterUI()
    End Sub

    Private Sub RefreshSortFilterUI()
        sortTextBox.Text = c1DataViewSet1.Views(0).Sort
        filterTextBox.Text = c1DataViewSet1.Views(0).RowFilter
    End Sub

    Private Sub RefreshSearchConditionsGrid()
        searchCondGridView.Rows.Clear()
        Dim colNameColumn As DataGridViewComboBoxColumn = CType(searchCondGridView.Columns("ColumnName"), DataGridViewComboBoxColumn)
        colNameColumn.Items.Clear()
        For Each viewColumn As C1ViewColumn In c1DataViewSet1.Views(0).Columns
            colNameColumn.Items.Add(viewColumn.ColumnName)
        Next
        Dim primaryKey As C1ViewColumn() = c1DataViewSet1.Views(0).PrimaryKey
        pkLabel.Text = ""
        For i As Integer = 0 To primaryKey.Length - 1
            If i <> 0 Then
                pkLabel.Text += ", "
            End If
            pkLabel.Text += primaryKey(i).ColumnName
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        definitionTextBox.Text = c1DataViewSet1.Views(0).Definition
        rowOccurenceCombo.SelectedIndex = 0
        RefreshSortFilterUI()
    End Sub

    Private Sub button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button7.Click
        RefreshSortFilterUI()
    End Sub

    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
        c1DataViewSet1.Views(0).RowFilter = filterTextBox.Text
    End Sub

    Private Sub button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button4.Click
        ' Last DataGridView row is always empty, so we don't consider it
        Dim searchValueCount As Integer = searchCondGridView.Rows.Count - 1
        Dim valueArr(searchValueCount - 1) As Object
        For i As Integer = 0 To searchValueCount - 1
            ' It's not necessary to convert values from string
            ' to target type - FindByPrimaryKey will do it automatically
            valueArr(i) = searchCondGridView.Rows(i).Cells("Value").Value
        Next
        Dim idx As Integer
        Try
            idx = c1DataViewSet1.Views(0).FindByPrimaryKey(valueArr)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
        If idx < 0 Then
            MessageBox.Show("Not found")
        Else
            c1DataViewSet1.Views(0).Position = idx
        End If
    End Sub

    Private Sub button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button5.Click
        ' Last DataGridView row is always empty, so we don't consider it
        Dim searchValueCount As Integer = searchCondGridView.Rows.Count - 1
        Dim valueArr(searchValueCount - 1) As Object
        For i As Integer = 0 To searchValueCount - 1
            ' It's not necessary to convert values from string
            ' to target type - Find will do it automatically
            valueArr(i) = searchCondGridView.Rows(i).Cells("Value").Value
        Next

        Dim duplKey As DuplicateKeyOccurenceEnum = DuplicateKeyOccurenceEnum.Accidental
        Select Case CType(rowOccurenceCombo.SelectedItem, String)
            Case "First"
                duplKey = DuplicateKeyOccurenceEnum.First
            Case "Last"
                duplKey = DuplicateKeyOccurenceEnum.Last
            Case "Accidental"
                duplKey = DuplicateKeyOccurenceEnum.Accidental
        End Select
        Dim idx As Integer
        Try
            idx = c1DataViewSet1.Views(0).Find(valueArr, duplKey)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
        ' If row is not found, the returning index is negative value
        ' which is the bitwise complement of the index of the next row 
        ' which key is larger than specified.
        If idx < 0 Then
            Dim nearestPos As Integer = Not (idx)
            If nearestPos = c1DataViewSet1.Views(0).Count Then
                nearestPos -= 1
            End If
            c1DataViewSet1.Views(0).Position = nearestPos
            MessageBox.Show("Not found. Current row is set to nearest row.")
        Else
            c1DataViewSet1.Views(0).Position = idx
        End If
    End Sub

    Private Sub button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button6.Click
        ' Last DataGridView row is always empty, so we don't consider it
        Dim searchValueCount As Integer = searchCondGridView.Rows.Count - 1
        Dim valueArr(searchValueCount - 1) As Object
        Dim colNameArr(searchValueCount - 1) As String
        For i As Integer = 0 To searchValueCount - 1
            colNameArr(i) = CType(searchCondGridView.Rows(i).Cells("ColumnName").Value, String)
            ' It's not necessary to convert values from string
            ' to target type - Find will do it automatically
            valueArr(i) = searchCondGridView.Rows(i).Cells("Value").Value
        Next
        Dim idx As Integer
        Try
            idx = c1DataViewSet1.Views(0).Find(colNameArr, valueArr)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

        If idx < 0 Then
            MessageBox.Show("Not found")
        Else
            c1DataViewSet1.Views(0).Position = idx
        End If
    End Sub

    Private Sub c1ViewSet1_ViewListChanged(ByVal sender As System.Object, ByVal e As C1.C1DataExtender.C1ViewListChangedEventArgs) Handles c1DataViewSet1.ViewListChanged
        If e.View Is c1DataViewSet1.Views(0) Then
            If e.ListChangedType = ListChangedType.PropertyDescriptorChanged Or _
                e.ListChangedType = ListChangedType.PropertyDescriptorAdded Or _
                e.ListChangedType = ListChangedType.PropertyDescriptorDeleted _
                Then
                RefreshSearchConditionsGrid()
            End If
        End If
    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        c1DataViewSet1.Views(0).Sort = sortTextBox.Text
    End Sub

    Private Sub c1DataViewSet1_ConnectionOpening(ByVal sender As System.Object, ByVal e As C1.C1DataExtender.ConnectionOpeningEventArgs) Handles c1DataViewSet1.ConnectionOpening
        Dim csb As New OleDbConnectionStringBuilder(e.Connection.ConnectionString)
        ' If connection string specified in application resources points to a non-existant database file
        ' then use a database from a default sample database location.
        If Not File.Exists(csb.DataSource) Then
            csb.DataSource = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), _
                     "ComponentOne Samples\Common\C1NWind.mdb")
            e.Connection.ConnectionString = csb.ToString()
        End If
    End Sub
End Class
