using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace Sorting
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.TabPage tabPage3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox textSort;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TabPage tabPageSource;
        private System.Windows.Forms.ComboBox comboFields;
        private System.Windows.Forms.ComboBox comboSortMode;
        private System.Windows.Forms.Button buttonAddSort;
        private System.Windows.Forms.ListBox listSorts;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.ComboBox comboTables;
        private System.Windows.Forms.Button buttonRemove;
        private System.Windows.Forms.Button buttonDown;
        private System.Windows.Forms.Button buttonUp;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.DataGrid dataGrid1;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.ListBox listFilters;
        private System.Windows.Forms.Label label10;
        private System.Windows.Forms.TextBox textFilter;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Label label12;
        private System.Windows.Forms.ComboBox comboFields2;
        private System.Windows.Forms.Label label13;
        private System.Windows.Forms.Label label14;
        private System.Windows.Forms.Button buttonRemoveFilter;
        private System.Windows.Forms.Button buttonAddFilter;
        private System.Windows.Forms.ComboBox comboOperator;
        private System.Windows.Forms.ComboBox comboValue;
        private C1.Data.C1DataSet c1DataSet1;
        private C1.Data.C1DataView c1DataView1;
        private C1.Data.C1SchemaDef c1SchemaDef1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPageSource = new System.Windows.Forms.TabPage();
            this.label7 = new System.Windows.Forms.Label();
            this.comboTables = new System.Windows.Forms.ComboBox();
            this.label6 = new System.Windows.Forms.Label();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.comboValue = new System.Windows.Forms.ComboBox();
            this.label14 = new System.Windows.Forms.Label();
            this.comboOperator = new System.Windows.Forms.ComboBox();
            this.label13 = new System.Windows.Forms.Label();
            this.buttonAddFilter = new System.Windows.Forms.Button();
            this.comboFields2 = new System.Windows.Forms.ComboBox();
            this.label12 = new System.Windows.Forms.Label();
            this.buttonRemoveFilter = new System.Windows.Forms.Button();
            this.textFilter = new System.Windows.Forms.TextBox();
            this.label10 = new System.Windows.Forms.Label();
            this.listFilters = new System.Windows.Forms.ListBox();
            this.label9 = new System.Windows.Forms.Label();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.buttonRemove = new System.Windows.Forms.Button();
            this.buttonDown = new System.Windows.Forms.Button();
            this.buttonUp = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.buttonAddSort = new System.Windows.Forms.Button();
            this.label5 = new System.Windows.Forms.Label();
            this.comboSortMode = new System.Windows.Forms.ComboBox();
            this.comboFields = new System.Windows.Forms.ComboBox();
            this.label4 = new System.Windows.Forms.Label();
            this.textSort = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.listSorts = new System.Windows.Forms.ListBox();
            this.tabPage3 = new System.Windows.Forms.TabPage();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.c1DataView1 = new C1.Data.C1DataView();
            this.c1DataSet1 = new C1.Data.C1DataSet();
            this.c1SchemaDef1 = new C1.Data.C1SchemaDef();
            this.tabControl1.SuspendLayout();
            this.tabPageSource.SuspendLayout();
            this.tabPage2.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.tabPage3.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DataView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DataSet1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1SchemaDef1)).BeginInit();
            this.SuspendLayout();
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPageSource);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage3);
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(0, 0);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(488, 317);
            this.tabControl1.TabIndex = 0;
            // 
            // tabPageSource
            // 
            this.tabPageSource.Controls.Add(this.label7);
            this.tabPageSource.Controls.Add(this.comboTables);
            this.tabPageSource.Controls.Add(this.label6);
            this.tabPageSource.Location = new System.Drawing.Point(4, 22);
            this.tabPageSource.Name = "tabPageSource";
            this.tabPageSource.Size = new System.Drawing.Size(480, 291);
            this.tabPageSource.TabIndex = 3;
            this.tabPageSource.Text = "Select DataSource";
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(8, 72);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(100, 16);
            this.label7.TabIndex = 3;
            this.label7.Text = "Table view name:";
            // 
            // comboTables
            // 
            this.comboTables.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboTables.Location = new System.Drawing.Point(8, 88);
            this.comboTables.Name = "comboTables";
            this.comboTables.Size = new System.Drawing.Size(232, 21);
            this.comboTables.TabIndex = 2;
            this.comboTables.SelectedIndexChanged += new System.EventHandler(this.comboTables_SelectedIndexChanged);
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(8, 24);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(328, 32);
            this.label6.TabIndex = 1;
            this.label6.Text = "Please select a table view from the list below. Then create sort/filter condition" +
                "s on Sorting/Filtering tabs.";
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.groupBox2);
            this.tabPage2.Controls.Add(this.buttonRemoveFilter);
            this.tabPage2.Controls.Add(this.textFilter);
            this.tabPage2.Controls.Add(this.label10);
            this.tabPage2.Controls.Add(this.listFilters);
            this.tabPage2.Controls.Add(this.label9);
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Size = new System.Drawing.Size(480, 291);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Filtering";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.comboValue);
            this.groupBox2.Controls.Add(this.label14);
            this.groupBox2.Controls.Add(this.comboOperator);
            this.groupBox2.Controls.Add(this.label13);
            this.groupBox2.Controls.Add(this.buttonAddFilter);
            this.groupBox2.Controls.Add(this.comboFields2);
            this.groupBox2.Controls.Add(this.label12);
            this.groupBox2.Location = new System.Drawing.Point(240, 40);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(232, 168);
            this.groupBox2.TabIndex = 13;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "New filter";
            // 
            // comboValue
            // 
            this.comboValue.Location = new System.Drawing.Point(80, 88);
            this.comboValue.Name = "comboValue";
            this.comboValue.Size = new System.Drawing.Size(136, 21);
            this.comboValue.TabIndex = 6;
            // 
            // label14
            // 
            this.label14.Location = new System.Drawing.Point(8, 88);
            this.label14.Name = "label14";
            this.label14.Size = new System.Drawing.Size(56, 23);
            this.label14.TabIndex = 5;
            this.label14.Text = "Value:";
            // 
            // comboOperator
            // 
            this.comboOperator.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboOperator.Items.AddRange(new object[] {
            "= (equals to)",
            "> (greater than)",
            "< (less than)",
            "<> (not equal to)"});
            this.comboOperator.Location = new System.Drawing.Point(80, 56);
            this.comboOperator.Name = "comboOperator";
            this.comboOperator.Size = new System.Drawing.Size(136, 21);
            this.comboOperator.TabIndex = 4;
            // 
            // label13
            // 
            this.label13.Location = new System.Drawing.Point(8, 56);
            this.label13.Name = "label13";
            this.label13.Size = new System.Drawing.Size(56, 16);
            this.label13.TabIndex = 3;
            this.label13.Text = "Operator:";
            // 
            // buttonAddFilter
            // 
            this.buttonAddFilter.Location = new System.Drawing.Point(136, 128);
            this.buttonAddFilter.Name = "buttonAddFilter";
            this.buttonAddFilter.Size = new System.Drawing.Size(75, 23);
            this.buttonAddFilter.TabIndex = 2;
            this.buttonAddFilter.Text = "Add";
            this.buttonAddFilter.Click += new System.EventHandler(this.buttonAddFilter_Click);
            // 
            // comboFields2
            // 
            this.comboFields2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboFields2.Location = new System.Drawing.Point(56, 24);
            this.comboFields2.Name = "comboFields2";
            this.comboFields2.Size = new System.Drawing.Size(160, 21);
            this.comboFields2.TabIndex = 1;
            this.comboFields2.SelectedIndexChanged += new System.EventHandler(this.comboFields2_SelectedIndexChanged);
            // 
            // label12
            // 
            this.label12.Location = new System.Drawing.Point(8, 24);
            this.label12.Name = "label12";
            this.label12.Size = new System.Drawing.Size(32, 23);
            this.label12.TabIndex = 0;
            this.label12.Text = "Field";
            // 
            // buttonRemoveFilter
            // 
            this.buttonRemoveFilter.Location = new System.Drawing.Point(168, 24);
            this.buttonRemoveFilter.Name = "buttonRemoveFilter";
            this.buttonRemoveFilter.Size = new System.Drawing.Size(56, 23);
            this.buttonRemoveFilter.TabIndex = 4;
            this.buttonRemoveFilter.Text = "Remove";
            this.buttonRemoveFilter.Click += new System.EventHandler(this.buttonRemoveFilter_Click);
            // 
            // textFilter
            // 
            this.textFilter.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textFilter.Location = new System.Drawing.Point(8, 264);
            this.textFilter.Name = "textFilter";
            this.textFilter.ReadOnly = true;
            this.textFilter.Size = new System.Drawing.Size(464, 20);
            this.textFilter.TabIndex = 3;
            // 
            // label10
            // 
            this.label10.Location = new System.Drawing.Point(8, 240);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(384, 16);
            this.label10.TabIndex = 2;
            this.label10.Text = "RowFilter property value (this string is assigned to dataView1.RowFilter property" +
                "):";
            // 
            // listFilters
            // 
            this.listFilters.Location = new System.Drawing.Point(8, 24);
            this.listFilters.Name = "listFilters";
            this.listFilters.Size = new System.Drawing.Size(152, 212);
            this.listFilters.TabIndex = 1;
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(8, 8);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(112, 16);
            this.label9.TabIndex = 0;
            this.label9.Text = "Applied filters:";
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.buttonRemove);
            this.tabPage1.Controls.Add(this.buttonDown);
            this.tabPage1.Controls.Add(this.buttonUp);
            this.tabPage1.Controls.Add(this.groupBox1);
            this.tabPage1.Controls.Add(this.textSort);
            this.tabPage1.Controls.Add(this.label3);
            this.tabPage1.Controls.Add(this.label2);
            this.tabPage1.Controls.Add(this.listSorts);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Size = new System.Drawing.Size(480, 291);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Sorting";
            // 
            // buttonRemove
            // 
            this.buttonRemove.Location = new System.Drawing.Point(168, 24);
            this.buttonRemove.Name = "buttonRemove";
            this.buttonRemove.Size = new System.Drawing.Size(56, 23);
            this.buttonRemove.TabIndex = 8;
            this.buttonRemove.Text = "Remove";
            this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click);
            // 
            // buttonDown
            // 
            this.buttonDown.Location = new System.Drawing.Point(168, 88);
            this.buttonDown.Name = "buttonDown";
            this.buttonDown.Size = new System.Drawing.Size(56, 24);
            this.buttonDown.TabIndex = 7;
            this.buttonDown.Text = "Down";
            this.buttonDown.Click += new System.EventHandler(this.buttonDown_Click);
            // 
            // buttonUp
            // 
            this.buttonUp.Location = new System.Drawing.Point(168, 56);
            this.buttonUp.Name = "buttonUp";
            this.buttonUp.Size = new System.Drawing.Size(56, 24);
            this.buttonUp.TabIndex = 6;
            this.buttonUp.Text = "Up";
            this.buttonUp.Click += new System.EventHandler(this.buttonUp_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.buttonAddSort);
            this.groupBox1.Controls.Add(this.label5);
            this.groupBox1.Controls.Add(this.comboSortMode);
            this.groupBox1.Controls.Add(this.comboFields);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Location = new System.Drawing.Point(232, 40);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(240, 136);
            this.groupBox1.TabIndex = 5;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "New sort field";
            // 
            // buttonAddSort
            // 
            this.buttonAddSort.Location = new System.Drawing.Point(144, 88);
            this.buttonAddSort.Name = "buttonAddSort";
            this.buttonAddSort.Size = new System.Drawing.Size(75, 23);
            this.buttonAddSort.TabIndex = 4;
            this.buttonAddSort.Text = "Add";
            this.buttonAddSort.Click += new System.EventHandler(this.buttonAddSort_Click);
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(8, 56);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(100, 23);
            this.label5.TabIndex = 3;
            this.label5.Text = "Order:";
            // 
            // comboSortMode
            // 
            this.comboSortMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboSortMode.Items.AddRange(new object[] {
            "ASC",
            "DESC"});
            this.comboSortMode.Location = new System.Drawing.Point(144, 56);
            this.comboSortMode.Name = "comboSortMode";
            this.comboSortMode.Size = new System.Drawing.Size(80, 21);
            this.comboSortMode.TabIndex = 2;
            // 
            // comboFields
            // 
            this.comboFields.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboFields.Location = new System.Drawing.Point(64, 24);
            this.comboFields.Name = "comboFields";
            this.comboFields.Size = new System.Drawing.Size(160, 21);
            this.comboFields.TabIndex = 1;
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(8, 24);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(40, 16);
            this.label4.TabIndex = 0;
            this.label4.Text = "Field:";
            // 
            // textSort
            // 
            this.textSort.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textSort.Location = new System.Drawing.Point(8, 264);
            this.textSort.Name = "textSort";
            this.textSort.ReadOnly = true;
            this.textSort.Size = new System.Drawing.Size(464, 20);
            this.textSort.TabIndex = 4;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(8, 240);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(368, 16);
            this.label3.TabIndex = 3;
            this.label3.Text = "Sort property value (this string is assigned to dataView1.Sort property):";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(8, 8);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(168, 16);
            this.label2.TabIndex = 2;
            this.label2.Text = "Applied sort criterias:";
            // 
            // listSorts
            // 
            this.listSorts.Location = new System.Drawing.Point(8, 24);
            this.listSorts.Name = "listSorts";
            this.listSorts.Size = new System.Drawing.Size(152, 212);
            this.listSorts.TabIndex = 1;
            // 
            // tabPage3
            // 
            this.tabPage3.Controls.Add(this.dataGrid1);
            this.tabPage3.Location = new System.Drawing.Point(4, 22);
            this.tabPage3.Name = "tabPage3";
            this.tabPage3.Size = new System.Drawing.Size(480, 291);
            this.tabPage3.TabIndex = 2;
            this.tabPage3.Text = "Resultset";
            // 
            // dataGrid1
            // 
            this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid1.DataMember = "";
            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.Location = new System.Drawing.Point(8, 8);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.Size = new System.Drawing.Size(464, 272);
            this.dataGrid1.TabIndex = 2;
            // 
            // c1DataView1
            // 
            this.c1DataView1.DataSet = this.c1DataSet1;
            this.c1DataView1.TableName = "";
            this.c1DataView1.TableViewName = "";
            // 
            // c1DataSet1
            // 
            this.c1DataSet1.DataLibrary = "";
            this.c1DataSet1.DataLibraryUrl = "";
            this.c1DataSet1.DataSetDef = "DataSet1";
            this.c1DataSet1.Locale = new System.Globalization.CultureInfo("en-US");
            this.c1DataSet1.Name = "c1DataSet1";
            this.c1DataSet1.SchemaClassName = "";
            this.c1DataSet1.SchemaDef = this.c1SchemaDef1;
            // 
            // c1SchemaDef1
            // 
            this.c1SchemaDef1.DummyImage = ((System.Drawing.Bitmap)(resources.GetObject("c1SchemaDef1.DummyImage")));
            this.c1SchemaDef1.CreateSchema += new System.EventHandler(this.c1SchemaDef1_CreateSchema);
            this.c1SchemaDef1.SerializedSchemaXML = this.c1SchemaDef1.GetSchemaFromResources(resources, "c1SchemaDef1", this);
            this.c1SchemaDef1.SerializedDesignTimeOptionsXML = this.c1SchemaDef1.GetDesignTimeOptionsFromResources(resources, "c1SchemaDef1", this);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(488, 317);
            this.Controls.Add(this.tabControl1);
            this.Name = "Form1";
            this.Text = "Using C1DataView: Filtering and sorting";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.tabControl1.ResumeLayout(false);
            this.tabPageSource.ResumeLayout(false);
            this.tabPage2.ResumeLayout(false);
            this.tabPage2.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage1.PerformLayout();
            this.groupBox1.ResumeLayout(false);
            this.tabPage3.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DataView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DataSet1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1SchemaDef1)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            c1DataSet1.Fill();
            for (int i = 0; i < c1DataSet1.TableViews.Count; i++)
                comboTables.Items.Add(c1DataSet1.TableViews[i].SchemaTable.Name);
        }

        private void comboTables_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                listSorts.Items.Clear();
                listFilters.Items.Clear();

                c1DataView1.Sort = "";
                c1DataView1.RowFilter = "";

                c1DataView1.TableViewName = comboTables.SelectedItem.ToString();

                if (c1DataView1.TableView != null)
                {
                    comboFields.Items.Clear();
                    comboFields2.Items.Clear();
                    for (int i = 0; i < c1DataView1.TableView.Fields.Count; i++)
                    {
                        comboFields.Items.Add(c1DataView1.TableView.Fields[i].Name);
                        comboFields2.Items.Add(c1DataView1.TableView.Fields[i].Name);
                    }
                }

                RefreshSort();
                RefreshFilter();
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void buttonAddSort_Click(object sender, System.EventArgs e)
        {
            if (comboFields.SelectedIndex != -1)
            {
                string text = comboFields.SelectedItem.ToString();
                if (comboSortMode.SelectedIndex > -1)
                    text += " " + comboSortMode.SelectedItem.ToString();
                listSorts.Items.Add(text);
                RefreshSort();
            }
        }

        private void buttonUp_Click(object sender, System.EventArgs e)
        {
            if (listSorts.SelectedIndex > 0)
            {
                int idx = listSorts.SelectedIndex;
                string sort1 = listSorts.Items[idx].ToString();
                string sort2 = listSorts.Items[idx - 1].ToString();
                listSorts.Items[idx - 1] = sort1;
                listSorts.Items[idx] = sort2;
                RefreshSort();
            }
        }

        private void buttonDown_Click(object sender, System.EventArgs e)
        {
            if (listSorts.SelectedIndex < listSorts.Items.Count - 1)
            {
                int idx = listSorts.SelectedIndex;
                string sort1 = listSorts.Items[idx].ToString();
                string sort2 = listSorts.Items[idx + 1].ToString();
                listSorts.Items[idx + 1] = sort1;
                listSorts.Items[idx] = sort2;
                RefreshSort();
            }
        }

        private void buttonRemove_Click(object sender, System.EventArgs e)
        {
            if (listSorts.SelectedIndex > -1)
                listSorts.Items.RemoveAt(listSorts.SelectedIndex);
            RefreshSort();
        }

        void RefreshSort()
        {
            if (c1DataView1.TableView == null)
                return;

            textSort.Clear();

            for (int i = 0; i < listSorts.Items.Count; i++)
            {
                if (textSort.Text != "")
                    textSort.Text += ", ";
                textSort.Text += listSorts.Items[i].ToString();
            }

            c1DataView1.Sort = textSort.Text;

            // Need to call Refresh because c1DataView1.AutoRefresh=false
            c1DataView1.Refresh();

            dataGrid1.DataSource = null;
            dataGrid1.DataSource = c1DataView1;
        }

        void RefreshFilter()
        {
            if (c1DataView1.TableView == null)
                return;

            textFilter.Clear();

            for (int i = 0; i < listFilters.Items.Count; i++)
            {
                if (textFilter.Text != "")
                    textFilter.Text += " AND ";
                textFilter.Text += listFilters.Items[i].ToString();
            }

            c1DataView1.RowFilter = textFilter.Text;

            // Need to call Refresh because c1DataView1.AutoRefresh=false
            c1DataView1.Refresh();

            dataGrid1.DataSource = null;
            dataGrid1.DataSource = c1DataView1;
        }

        private void buttonAddFilter_Click(object sender, System.EventArgs e)
        {
            if (comboFields2.SelectedIndex > -1 && comboOperator.SelectedIndex > -1 && comboValue.Text != "")
            {
                string colName = comboFields2.Text;
                string strVal = comboValue.Text;
                int operatorId = comboOperator.SelectedIndex;
                if (c1DataView1.TableView.Fields[colName].Type == typeof(string))
                {
                    bool found = true;
                    int start = 0;
                    while (found)
                    {
                        found = false;
                        int idx = strVal.IndexOf("'", start);
                        if (idx > -1)
                        {
                            strVal = strVal.Substring(0, idx) + "''" + strVal.Substring(idx + 1);
                            start = idx + 2;
                            found = true;
                        }
                    }
                    strVal = "'" + strVal + "'";
                }
                if (c1DataView1.TableView.Fields[colName].Type == typeof(DateTime))
                    strVal = "#" + strVal + "#";

                string filterText = colName;

                switch (operatorId)
                {
                    case 0: filterText += " = "; break;
                    case 1: filterText += " > "; break;
                    case 2: filterText += " < "; break;
                    case 3: filterText += " <> "; break;
                }

                filterText += strVal;
                listFilters.Items.Add(filterText);
                RefreshFilter();
            }
            else
                MessageBox.Show("Not enough information to add a filter. Specify a field, operator and value");
        }

        private void buttonRemoveFilter_Click(object sender, System.EventArgs e)
        {
            if (listFilters.SelectedIndex > -1)
            {
                listFilters.Items.RemoveAt(listFilters.SelectedIndex);
                RefreshFilter();
            }
        }

        private void comboFields2_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            RefreshLookupValues();
        }

        void RefreshLookupValues()
        {
            comboValue.Items.Clear();

            if (comboFields2.SelectedIndex < 0)
                return;

            string colName = comboFields2.Items[comboFields2.SelectedIndex].ToString();
            C1.Data.C1DataView dv1 = new C1.Data.C1DataView();
            dv1.DataSet = c1DataSet1;
            dv1.TableViewName = comboTables.SelectedItem.ToString();
            dv1.Sort = colName + " ASC";


            dv1.Refresh();
            for (int i = 0; i < dv1.Count; i++)
            {
                C1.Data.C1DataRow row = dv1[i];
                object val = row[colName];
                if (val != null)
                {
                    string strVal = val.ToString();
                    bool found = false;
                    for (int j = 0; j < comboValue.Items.Count; j++)
                    {
                        if (comboValue.Items[j].ToString() == strVal)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                        comboValue.Items.Add(strVal);
                }
            }
        }

        #region Switch to C1NWind.mdb database

        private void c1SchemaDef1_CreateSchema(object sender, EventArgs e)
        {
            string dbPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) +
                "\\ComponentOne Samples\\Common\\C1NWind.mdb";
            if (File.Exists(dbPath))
            {
                C1.Data.SchemaObjects.Connection conn = c1SchemaDef1.Schema.Connections[0];
                OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder(conn.ConnectionString);
                csb.DataSource = dbPath;
                conn.ConnectionString = csb.ToString();
            }
        }

        #endregion
    }
}
