Esempio n. 1
0
        private void Search()
        {
            bool caseSensitive = SearchCaseSensitive.IsChecked.GetValueOrDefault(false);
            bool isRegex       = SearchRegex.IsChecked.GetValueOrDefault(false);

            string searchTerm = SearchBox.Text;

            if (!isRegex && !caseSensitive)
            {
                searchTerm = searchTerm.ToLower();
            }

            for (int rowIndex = RowData.SelectedIndex + 1; rowIndex < RowData.Items.Count; rowIndex++)
            {
                DBRow row = (DBRow)RowData.Items[rowIndex];

                for (int col = 0; col < row.NumColumns; col++)
                {
                    object value = row[col];
                    if (value == null)
                    {
                        continue;
                    }

                    string strValue = value.ToString();

                    bool match = false;
                    if (SearchRegex.IsChecked.GetValueOrDefault(false))
                    {
                        match = System.Text.RegularExpressions.Regex.Match(
                            strValue,
                            searchTerm,
                            caseSensitive
                                ? System.Text.RegularExpressions.RegexOptions.None
                                : System.Text.RegularExpressions.RegexOptions.IgnoreCase
                            ).Success;
                    }
                    else
                    {
                        string haystack = strValue;
                        if (!caseSensitive)
                        {
                            haystack = haystack.ToLower();
                        }

                        match = haystack.Contains(searchTerm);
                    }

                    if (match)
                    {
                        ListViewItem viewItem = (ListViewItem)RowData.ItemContainerGenerator.ContainerFromItem(row);
                        if (viewItem == null)
                        {
                            RowData.ScrollIntoView(row);
                            viewItem = (ListViewItem)RowData.ItemContainerGenerator.ContainerFromItem(row);
                        }

                        if (viewItem != null)
                        {
                            RowData.SelectedItem = null;
                            viewItem.IsSelected  = true;
                        }

                        return;
                    }
                }
            }

            MessageBox.Show("No match.");
        }