Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class frmDataViews
    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 DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents txtFilter1 As System.Windows.Forms.TextBox
    Friend WithEvents cboSort1 As System.Windows.Forms.ComboBox
    Friend WithEvents cboRowState1 As System.Windows.Forms.ComboBox
    Friend WithEvents chkDesc1 As System.Windows.Forms.CheckBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents btnApply1 As System.Windows.Forms.Button
    Friend WithEvents btnApply2 As System.Windows.Forms.Button
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents chkDesc2 As System.Windows.Forms.CheckBox
    Friend WithEvents cboRowState2 As System.Windows.Forms.ComboBox
    Friend WithEvents cboSort2 As System.Windows.Forms.ComboBox
    Friend WithEvents txtFilter2 As System.Windows.Forms.TextBox
    Friend WithEvents DataGrid2 As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.txtFilter1 = New System.Windows.Forms.TextBox
        Me.cboSort1 = New System.Windows.Forms.ComboBox
        Me.cboRowState1 = New System.Windows.Forms.ComboBox
        Me.chkDesc1 = New System.Windows.Forms.CheckBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.btnApply1 = New System.Windows.Forms.Button
        Me.btnApply2 = New System.Windows.Forms.Button
        Me.Label4 = New System.Windows.Forms.Label
        Me.Label5 = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.chkDesc2 = New System.Windows.Forms.CheckBox
        Me.cboRowState2 = New System.Windows.Forms.ComboBox
        Me.cboSort2 = New System.Windows.Forms.ComboBox
        Me.txtFilter2 = New System.Windows.Forms.TextBox
        Me.DataGrid2 = New System.Windows.Forms.DataGrid
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DataGrid2, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DataGrid1
        '
        Me.DataGrid1.CaptionText = "Domyślny widok danych"
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(8, 16)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(392, 192)
        Me.DataGrid1.TabIndex = 0
        '
        'txtFilter1
        '
        Me.txtFilter1.Location = New System.Drawing.Point(424, 40)
        Me.txtFilter1.Name = "txtFilter1"
        Me.txtFilter1.Size = New System.Drawing.Size(224, 20)
        Me.txtFilter1.TabIndex = 1
        Me.txtFilter1.Text = ""
        '
        'cboSort1
        '
        Me.cboSort1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSort1.Location = New System.Drawing.Point(424, 88)
        Me.cboSort1.Name = "cboSort1"
        Me.cboSort1.Size = New System.Drawing.Size(144, 21)
        Me.cboSort1.TabIndex = 2
        '
        'cboRowState1
        '
        Me.cboRowState1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboRowState1.Location = New System.Drawing.Point(424, 136)
        Me.cboRowState1.Name = "cboRowState1"
        Me.cboRowState1.Size = New System.Drawing.Size(144, 21)
        Me.cboRowState1.TabIndex = 3
        '
        'chkDesc1
        '
        Me.chkDesc1.Location = New System.Drawing.Point(584, 88)
        Me.chkDesc1.Name = "chkDesc1"
        Me.chkDesc1.Size = New System.Drawing.Size(72, 24)
        Me.chkDesc1.TabIndex = 4
        Me.chkDesc1.Text = "Malejąco"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(424, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(40, 16)
        Me.Label1.TabIndex = 5
        Me.Label1.Text = "Filtr:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(424, 72)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(152, 16)
        Me.Label2.TabIndex = 6
        Me.Label2.Text = "Sortowanie według kolumny:"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(424, 120)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(72, 16)
        Me.Label3.TabIndex = 7
        Me.Label3.Text = "Stan wiersza:"
        '
        'btnApply1
        '
        Me.btnApply1.Location = New System.Drawing.Point(424, 176)
        Me.btnApply1.Name = "btnApply1"
        Me.btnApply1.Size = New System.Drawing.Size(144, 23)
        Me.btnApply1.TabIndex = 8
        Me.btnApply1.Text = "Zastosuj"
        '
        'btnApply2
        '
        Me.btnApply2.Location = New System.Drawing.Point(424, 384)
        Me.btnApply2.Name = "btnApply2"
        Me.btnApply2.Size = New System.Drawing.Size(144, 23)
        Me.btnApply2.TabIndex = 17
        Me.btnApply2.Text = "Zastosuj"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(424, 328)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(72, 16)
        Me.Label4.TabIndex = 16
        Me.Label4.Text = "Stan wiersza:"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(424, 280)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(152, 16)
        Me.Label5.TabIndex = 15
        Me.Label5.Text = "Sortowanie według kolumny:"
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(424, 232)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(40, 16)
        Me.Label6.TabIndex = 14
        Me.Label6.Text = "Filtr:"
        '
        'chkDesc2
        '
        Me.chkDesc2.Location = New System.Drawing.Point(584, 296)
        Me.chkDesc2.Name = "chkDesc2"
        Me.chkDesc2.Size = New System.Drawing.Size(72, 24)
        Me.chkDesc2.TabIndex = 13
        Me.chkDesc2.Text = "Malejąco"
        '
        'cboRowState2
        '
        Me.cboRowState2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboRowState2.Location = New System.Drawing.Point(424, 344)
        Me.cboRowState2.Name = "cboRowState2"
        Me.cboRowState2.Size = New System.Drawing.Size(144, 21)
        Me.cboRowState2.TabIndex = 12
        '
        'cboSort2
        '
        Me.cboSort2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cboSort2.Location = New System.Drawing.Point(424, 296)
        Me.cboSort2.Name = "cboSort2"
        Me.cboSort2.Size = New System.Drawing.Size(144, 21)
        Me.cboSort2.TabIndex = 11
        '
        'txtFilter2
        '
        Me.txtFilter2.Location = New System.Drawing.Point(424, 248)
        Me.txtFilter2.Name = "txtFilter2"
        Me.txtFilter2.Size = New System.Drawing.Size(224, 20)
        Me.txtFilter2.TabIndex = 10
        Me.txtFilter2.Text = ""
        '
        'DataGrid2
        '
        Me.DataGrid2.CaptionText = "Drugi widok danych"
        Me.DataGrid2.DataMember = ""
        Me.DataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid2.Location = New System.Drawing.Point(8, 224)
        Me.DataGrid2.Name = "DataGrid2"
        Me.DataGrid2.Size = New System.Drawing.Size(392, 192)
        Me.DataGrid2.TabIndex = 9
        '
        'frmDataViews
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(664, 437)
        Me.Controls.Add(Me.cboRowState2)
        Me.Controls.Add(Me.btnApply2)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.chkDesc2)
        Me.Controls.Add(Me.cboSort2)
        Me.Controls.Add(Me.txtFilter2)
        Me.Controls.Add(Me.DataGrid2)
        Me.Controls.Add(Me.btnApply1)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.chkDesc1)
        Me.Controls.Add(Me.cboRowState1)
        Me.Controls.Add(Me.cboSort1)
        Me.Controls.Add(Me.txtFilter1)
        Me.Controls.Add(Me.DataGrid1)
        Me.Name = "frmDataViews"
        Me.Text = "Dwa widoki danych"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DataGrid2, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private dsCustomers As New DataSet
    Private dvView2 As DataView

    Private Sub frmDataViews_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        Dim col As DataColumn

        ' Inicjalizacja obiektu DataAdapter
        Dim daCustomers As SqlDataAdapter = New SqlDataAdapter( _
           "select * from tblKlienci", _
           "server=localhost;uid=sa;database=Nowinki")

        ' Wypełnianie tabeli (tylko jednej)
        daCustomers.Fill(dsCustomers, "Klienci")

        ' Tworzenie drugiego widoku danych
        dvView2 = New DataView(dsCustomers.Tables("Klienci"), _
         "", "Nazwisko", DataViewRowState.CurrentRows)

        ' Wypełnianie listy z nazwami kolumn
        For Each col In dsCustomers.Tables("Klienci").Columns
            cboSort1.Items.Add(col.ColumnName)
            cboSort2.Items.Add(col.ColumnName)
        Next

        ' Wypełnianie listy z wartościami wyliczenia DataViewRowState 
        Dim names As String()
        names = DataViewRowState.None.GetNames(DataViewRowState.None.GetType)
        For i = 0 To names.GetUpperBound(0)
            cboRowState1.Items.Add(names(i))
            cboRowState2.Items.Add(names(i))
        Next

        ' Ustawianie wartości domyślnych
        txtFilter1.Text = ""
        txtFilter2.Text = ""
        cboSort1.SelectedItem = "ID"
        cboSort2.SelectedItem = "ID"
        chkDesc1.Checked = False
        chkDesc2.Checked = False
        cboRowState1.SelectedItem = "CurrentRows"
        cboRowState2.SelectedItem = "CurrentRows"

        dsCustomers.Tables("Klienci").DefaultView.Sort = "ID"
        dvView2.Sort = "ID"

        ' Wiązanie siatek z tabelą
        DataGrid1.DataSource = dsCustomers.Tables("Klienci").DefaultView
        DataGrid2.DataSource = dvView2

    End Sub

    Private Sub btnApply1_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnApply1.Click
        Dim sort As String
        Dim rowState As DataViewRowState

        ' Ustawianie filtra
        dsCustomers.Tables("Klienci").DefaultView.RowFilter = txtFilter1.Text

        ' Ustawianie sortowania
        sort = cboSort1.SelectedItem
        If chkDesc1.Checked Then
            sort = sort & " DESC"
        End If
        dsCustomers.Tables("Klienci").DefaultView.Sort = sort

        ' Ustawianie stanu wiersza
        dsCustomers.Tables("Klienci").DefaultView.RowStateFilter = _
                    rowState.Parse(rowState.GetType, cboRowState1.SelectedItem)
    End Sub


    Private Sub btnApply2_Click(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles btnApply2.Click
        Dim sort As String
        Dim rowState As DataViewRowState

        ' Ustawianie filtra
        dvView2.RowFilter = txtFilter2.Text

        ' Ustawianie sortowania
        sort = cboSort2.SelectedItem
        If chkDesc2.Checked Then
            sort = sort & " DESC"
        End If
        dvView2.Sort = sort

        ' Ustawianie stanu wiersza
        dvView2.RowStateFilter = rowState.Parse(rowState.GetType, cboRowState2.SelectedItem)
    End Sub

End Class
