コード例 #1
0
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (this.cmbSearch.Text == "")
            {
                MessageBox.Show("Please enter the search text", Constants.msgAttention, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.cmbSearch.Focus();
                return;
            }

            if (this.cmbColumns.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a column to search", Constants.msgAttention, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.cmbColumns.Focus();
                return;
            }

            // store the search value in the combo
            if (this.cmbSearch.Items.Contains(this.cmbSearch.Text) == false)
            {
                this.cmbSearch.Items.Add(this.cmbSearch.Text);
            }


            int searchColIndex = ((KeyValuePair <int, string>)cmbColumns.SelectedItem).Key;

            string search_text = this.cmbSearch.Text;
            string s           = "";
            bool   found       = false;


            // if smth has changed in the search we start from the 1st row
            if ((this.LastSearched_Column != searchColIndex) || (this.LastSearched_Exact != this.check_Exact.Checked) || (this.LastSearched_String != search_text))
            {
                this.LastSearched_Row    = 0;
                this.LastFound_Column    = 0;
                this.LastSearched_Column = searchColIndex;
                this.LastSearched_String = search_text;
                this.LastSearched_Exact  = this.check_Exact.Checked;

                this.AnyMatch = false;
            }

            if (searchColIndex != -1)
            {   // look in a specific column
                for (int k = LastSearched_Row; k < this.dgvToBeSearchedMeta.dgv.RowCount; k++)
                {
                    if (this.dgvToBeSearchedMeta.dgv.Rows[k].Visible == false) // only check visible rows
                    {
                        continue;
                    }

                    s = (this.dgvToBeSearchedMeta.dgv.Rows[k].Cells[searchColIndex].Value == null) ? "" : this.dgvToBeSearchedMeta.dgv.Rows[k].Cells[searchColIndex].Value.ToString();
                    if (s == "")
                    {
                        continue;
                    }


                    if (StaticFunctions.IsSubstring(s, search_text, this.check_Exact.Checked))
                    {
                        found            = true;
                        LastSearched_Row = k + 1;
                        if (LastSearched_Row >= this.dgvToBeSearchedMeta.dgv.RowCount)
                        {
                            LastSearched_Row = 0;
                        }

                        this.dgvToBeSearchedMeta.dgv.CurrentCell = this.dgvToBeSearchedMeta.dgv.Rows[k].Cells[searchColIndex];

                        break;
                    }
                }
            }
            else // the search col = -1, i.e. we need to check all columns
            {
                int[] searchable_Cols = null; //the columns which are visible and which are not of bool type
                int   u = 0;
                for (int i = 0; i < this.dgvToBeSearchedMeta.dgv.ColumnCount; i++)
                {
                    if ((this.dgvToBeSearchedMeta.dgv.Columns[i].Visible == true) && !(this.dgvToBeSearchedMeta.dgv.Columns[i].CellTemplate is DataGridViewCheckBoxCell))
                    {
                        u++;
                        Array.Resize(ref searchable_Cols, u);
                        searchable_Cols[u - 1] = i;
                    }
                }

                if (searchable_Cols == null)
                {
                    MessageBox.Show("There are no columns in the grid.", Constants.msgWarning, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }


                for (int k = LastSearched_Row; k < this.dgvToBeSearchedMeta.dgv.RowCount; k++)
                {
                    if (this.dgvToBeSearchedMeta.dgv.Rows[k].Visible == false) //We only check visible rows
                    {
                        continue;
                    }

                    for (int i = this.LastFound_Column; i < searchable_Cols.Length; i++)
                    {
                        s = (this.dgvToBeSearchedMeta.dgv.Rows[k].Cells[searchable_Cols[i]].Value == null) ? "" : this.dgvToBeSearchedMeta.dgv.Rows[k].Cells[searchable_Cols[i]].Value.ToString();
                        if (s == "")
                        {
                            continue;
                        }

                        if (StaticFunctions.IsSubstring(s, search_text, this.check_Exact.Checked))
                        {
                            found = true;

                            if (i >= searchable_Cols.Length - 1)
                            {
                                this.LastFound_Column = 0;
                                LastSearched_Row      = k + 1;
                                if (LastSearched_Row >= this.dgvToBeSearchedMeta.dgv.RowCount)
                                {
                                    LastSearched_Row = 0;
                                }
                            }
                            else
                            {
                                LastFound_Column = i + 1;
                                LastSearched_Row = k;
                            }

                            this.dgvToBeSearchedMeta.dgv.CurrentCell = this.dgvToBeSearchedMeta.dgv.Rows[k].Cells[searchable_Cols[i]];

                            break;
                        }
                    }


                    if (found)
                    {
                        this.AnyMatch = true;
                        break;
                    }
                    else
                    {
                        this.LastFound_Column = 0;
                    }
                }
            }


            if (found == false)
            {
                LastSearched_Row = 0;
                if (this.AnyMatch == false)
                {
                    MessageBox.Show("Nothing was found", Constants.msgAttention, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("The search has reached the end of the grid", Constants.msgAttention, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
コード例 #2
0
        private void ReadGridColumns()
        {
            // reads the column names of the datagridview

            this.CleanColumnInfo();

            if (this.dgv == null || dgv.ColumnCount == 0)
            {
                return;
            }

            string s = "";
            int    u = 0; // will count only visible columns

            for (int k = 0; k < this.dgv.ColumnCount; k++)
            {
                if (this.dgv.Columns[k].Visible)
                {
                    u++;

                    if (this.FirstVisibleColumnIndex == -1)
                    {
                        this.FirstVisibleColumnIndex = k;
                    }

                    Array.Resize(ref col_indices, u);
                    col_indices[u - 1] = k;

                    Array.Resize(ref col_names, u);
                    col_names[u - 1] = this.dgv.Columns[k].Name;

                    Array.Resize(ref col_headerTexts, u);
                    col_headerTexts[u - 1] = (this.dgv.Columns[k].HeaderText == "") ? this.dgv.Columns[k].Name : this.dgv.Columns[k].HeaderText;

                    Array.Resize(ref col_valueTypes, u);
                    s = this.dgv.Columns[k].ValueType.Name.ToLower();

                    if (StaticFunctions.IsSubstring(s, Constants.types_Bool))
                    {
                        col_valueTypes[u - 1] = Constants.ValueType_Bool;
                    }
                    else if (StaticFunctions.IsSubstring(s, Constants.types_DateTime))
                    {
                        col_valueTypes[u - 1] = Constants.ValueType_Date;
                    }
                    else if (StaticFunctions.IsSubstring(s, Constants.types_Numeric))
                    {
                        col_valueTypes[u - 1] = Constants.ValueType_Numeric;
                    }
                    else
                    {
                        col_valueTypes[u - 1] = Constants.ValueType_String;
                    }
                }
            }

            // setting the property values
            ColumnValueTypeLength  = this.col_valueTypes.Length;
            ColumnHeaderTextLength = this.col_headerTexts.Length;
            ColumnNameLength       = this.col_names.Length;
            ColumnIndexLength      = this.col_indices.Length;
        }