Windows Form File /DBCommander/Modals/SelectionForm.cs (C#)
using System;  
using System.Collections;  
using System.ComponentModel;  
using System.Drawing;  
using System.Data;  
using System.Windows.Forms;  
using System.Text.RegularExpressions;  
using System.Globalization;  
  
namespace YariSoft.DBCommander.Modals  
{  
    public class SelectionForm : YariSoft.Utils.ModalBaseForm  
    {  
        public enum SelectOperationType { Select, Unselect, Filter };  
  
        #region Local variables  
        private DataGrid grid = null;  
        private DataView view = null;  
        private SelectOperationType operationType = SelectOperationType.Select;  
        private System.Windows.Forms.Label columnLabel;  
        private System.Windows.Forms.Label searchLabel;  
        private System.Windows.Forms.ListBox columnsListBox;  
        private System.Windows.Forms.ComboBox searchComboBox;  
        private System.ComponentModel.IContainer components = null;  
        #endregion  
  
        #region Constructor/Destructor  
        public SelectionForm()  
        {  
            InitializeComponent();  
        }  
  
        protected override void Dispose( bool disposing )  
        {  
            if( disposing )  
            {  
                if (components != null)  
                {  
                    components.Dispose();  
                }  
            }  
            base.Dispose( disposing );  
        }  
        #endregion  
  
        #region 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()  
        {  
            this.columnLabel = new System.Windows.Forms.Label();  
            this.searchLabel = new System.Windows.Forms.Label();  
            this.columnsListBox = new System.Windows.Forms.ListBox();  
            this.searchComboBox = new System.Windows.Forms.ComboBox();  
            this.SuspendLayout();  
            //    
            // columnLabel  
            //    
            this.columnLabel.AutoSize = true;  
            this.columnLabel.Location = new System.Drawing.Point(15, 54);  
            this.columnLabel.Name = "columnLabel";  
            this.columnLabel.Size = new System.Drawing.Size(60, 13);  
            this.columnLabel.TabIndex = 3;  
            this.columnLabel.Text = "for column:";  
            //    
            // searchLabel  
            //    
            this.searchLabel.AutoSize = true;  
            this.searchLabel.Location = new System.Drawing.Point(15, 6);  
            this.searchLabel.Name = "searchLabel";  
            this.searchLabel.Size = new System.Drawing.Size(92, 13);  
            this.searchLabel.TabIndex = 2;  
            this.searchLabel.Text = "Specify selection:";  
            //    
            // columnsListBox  
            //    
            this.columnsListBox.Location = new System.Drawing.Point(15, 70);  
            this.columnsListBox.Name = "columnsListBox";  
            this.columnsListBox.Size = new System.Drawing.Size(264, 186);  
            this.columnsListBox.TabIndex = 0;  
            this.columnsListBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.searchComboBox_KeyDown);  
            //    
            // searchComboBox  
            //    
            this.searchComboBox.Location = new System.Drawing.Point(15, 22);  
            this.searchComboBox.Name = "searchComboBox";  
            this.searchComboBox.Size = new System.Drawing.Size(264, 21);  
            this.searchComboBox.TabIndex = 1;  
            this.searchComboBox.Text = "*";  
            this.searchComboBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.searchComboBox_KeyDown);  
            //    
            // SelectionForm  
            //    
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);  
            this.ClientSize = new System.Drawing.Size(294, 295);  
            this.Controls.AddRange(new System.Windows.Forms.Control[] {  
                                                                          this.columnLabel,  
                                                                          this.searchLabel,  
                                                                          this.columnsListBox,  
                                                                          this.searchComboBox});  
            this.Name = "SelectionForm";  
            this.Closing += new System.ComponentModel.CancelEventHandler(this.SelectionForm_Closing);  
            this.Load += new System.EventHandler(this.SelectionForm_Load);  
            this.ResumeLayout(false);  
  
        }  
        #endregion  
  
        #region Public functions  
        public bool Init( DataGrid Grid, DataView View, SelectOperationType OperationType, ArrayList Selections )  
        {  
            this.grid = Grid;  
            this.view = View;  
  
            if( Selections == null ){  
                Selections = new ArrayList();  
            }  
            this.searchComboBox.DataSource = Selections;  
            if( Selections.Count > 0 ){  
                this.searchComboBox.Text = Selections[0].ToString();  
            }  
  
            this.operationType = OperationType;  
            switch( this.operationType ){  
                case SelectOperationType.Select:  
                    this.Text = "Select";  
                    break;  
                case SelectOperationType.Unselect:  
                    this.Text = "Unselect";  
                    break;  
                case SelectOperationType.Filter:  
                    this.Text = "Filter";  
                    break;  
            }  
      
            this.Text += " rows";  
              
            return this.FillColumnsListBox();  
        }  
        #endregion  
  
        #region Private functions  
        private bool FillColumnsListBox()  
        {  
            if( this.grid == null || this.grid.DataSource == null ){  
                return false;  
            }  
              
            bool found = false;  
  
            if( this.grid.TableStyles.Count > 0 ){  
                DataGridTableStyle style = this.grid.TableStyles[view.Table.TableName];  
                if( style != null ){  
                    if( style.MappingName == view.Table.TableName ){  
                        foreach( DataGridColumnStyle columnStyle in style.GridColumnStyles ){  
                            this.columnsListBox.Items.Add( columnStyle.HeaderText );  
                        }  
                        found = true;  
                    }  
                }  
            }  
            if( ! found ){  
                foreach( DataColumn column in view.Table.Columns ){  
                    this.columnsListBox.Items.Add( column.ColumnName );  
                }  
            }  
  
            if( this.columnsListBox.Items.Count > 0 ){  
                if( this.grid.CurrentCell.ColumnNumber > -1 &&  
                    this.columnsListBox.Items.Count > this.grid.CurrentCell.ColumnNumber ){  
                    this.columnsListBox.SelectedIndex = this.grid.CurrentCell.ColumnNumber;  
                }  
            } else {  
                this.columnsListBox.SelectedItem = this.columnsListBox.Items[0];  
            }  
  
            return true;  
        }  
  
        private void SelectionForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)  
        {  
            try{  
                if( this.DialogResult == DialogResult.OK ){      
                    this.Cursor = Cursors.WaitCursor;  
                    YariSoft.Utils.YMessages.ChangeCursor ( this.Cursor );  
                    this.Refresh();  
                    if( ! this.SearchRows() || ! this.UpdateSelections() ){  
                        e.Cancel = true;  
                        return;  
                    }  
                }  
            } finally {  
                this.Cursor = Cursors.Default;  
                YariSoft.Utils.YMessages.ChangeCursor ( this.Cursor );  
            }  
        }  
  
        private bool SearchRows()  
        {  
            try{  
                string colName = this.columnsListBox.Text;  
                if( this.grid.TableStyles.Count > 0 && this.grid.TableStyles[view.Table.TableName] != null ){  
                    bool found = false;  
                    foreach( DataGridColumnStyle columnStyle in this.grid.TableStyles[view.Table.TableName].GridColumnStyles ){  
                        if( columnStyle.HeaderText == this.columnsListBox.Text ){  
                            colName = columnStyle.MappingName;  
                            found = true;  
                            break;  
                        }  
                    }  
                    if( ! found ){  
                        YariSoft.Utils.YMessages.Show( "Column '" + this.columnsListBox.Text + "' not found in the grid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );  
                        return false;  
                    }  
                }  
  
                string mask    = this.searchComboBox.Text;  
                /*  
                if( mask[0]!= '*' ){  
                    mask = "^" + mask;  
                }  
  
                if( mask[mask.Length-1]!= '*' ){  
                    mask =  mask + "$";  
                }  
                */  
  
                mask = mask.Replace( "*", "." );  
                mask = mask.Replace( "?", "(.{1})" );  
                Regex r = new Regex( mask, RegexOptions.IgnoreCase | RegexOptions.Singleline );  
  
                ArrayList uniqColumns = null;  
                string rowFilter = "";  
                if( this.operationType == SelectOperationType.Filter ){  
                    uniqColumns = PrepareUniqColumns();  
                }  
                  
                bool firstFound = false;  
                for( int i = 0; i < view.Count; i++ ){  
                    MatchCollection m = r.Matches( view[i][colName].ToString() );  
                    if( m.Count > 0 ){  
                        if( ! firstFound ){  
                            this.grid.CurrentCell = new DataGridCell( i, this.grid.CurrentCell.ColumnNumber );  
                            firstFound = true;  
                        }  
  
                        switch( this.operationType ){  
                            case SelectOperationType.Select:  
                                this.grid.Select(i);  
                                break;  
                            case SelectOperationType.Unselect:  
                                this.grid.UnSelect(i);  
                                break;  
                            case SelectOperationType.Filter:  
                                this.AddExpressionToRowFilter( view[i], ref uniqColumns, ref rowFilter );  
                                break;  
                        }  
                    }  
                }  
                if( this.operationType == SelectOperationType.Filter ){  
                    view.RowFilter = rowFilter;  
                }  
  
            }catch( Exception Exp ){  
                YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );  
                return false;  
            }  
            return true;  
        }  
  
        private bool UpdateSelections()  
        {  
            ArrayList selections = ( ArrayList )this.searchComboBox.DataSource;  
            int index = selections.IndexOf( this.searchComboBox.Text );  
            if( index > -1 ){  
                selections.RemoveAt( index );  
            }  
            selections.Insert( 0,this.searchComboBox.Text );  
            return true;  
        }  
  
        private void SelectionForm_Load(object sender, System.EventArgs e)  
        {  
            this.searchComboBox.Select();          
        }  
  
        private void searchComboBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)  
        {  
            if( e.KeyData == Keys.Escape ){  
                this.Close();  
            }  
            if( e.KeyData == Keys.Enter ){                  
                this.DialogResult = DialogResult.OK;  
            }  
        }  
  
        private ArrayList PrepareUniqColumns()  
        {  
            ArrayList result = null;  
            DataColumn[] primaryKeys = this.view.Table.PrimaryKey;  
            if( primaryKeys != null ){  
                result = new ArrayList( primaryKeys );  
            }  
  
            if( result == null || result.Count == 0 ){  
                result = new ArrayList ( this.view.Table.Columns );  
            }  
            return result;  
        }  
  
        private void AddExpressionToRowFilter( DataRowView Row, ref ArrayList UniqColumns, ref string rowFilter )  
        {  
            string result = "";  
  
            bool found = false;  
            result += " (";  
            foreach( DataColumn column in UniqColumns ){  
                if( Row.Row[column] != null && Row.Row[column].ToString() != "" && ! ( Row.Row[column] is DateTime ) ){  
                    result += column.ColumnName + " = '" + Row.Row[column].ToString() + "' AND ";  
                    found = true;  
                }  
            }  
            if( ! found ){  
                return;  
            }  
            result = result.Substring( 0, result.Length - 4 );  
            result += " )";  
  
            if( rowFilter != "" ){  
                rowFilter += " OR ";  
            }  
  
            rowFilter += result;  
        }  
        #endregion  
    }  
}