void frmFind_SearchRequested(object sender, SearchEventArgs e) { var dgv = (sender as FrmSearch).SearchSource ?? dgvCurrent; Func <String, String, bool> comparer; Func <String, String> modifier; if (!e.MatchCase) { modifier = a => a.ToLower(); } else { modifier = a => a; } switch (e.SearchCriteria) { case SearchCriteria.Contains: comparer = (a, b) => modifier(a).Contains(modifier(b)); break; case SearchCriteria.StartsWith: default: comparer = (a, b) => modifier(a).StartsWith(modifier(b)); break; } bool fromBeginning = false; do { int index = dgv.SelectedCells.Count > 0 ? dgv.SelectedCells[0].RowIndex : -1; while (++index < dgv.Rows.Count) { var entry = dgv.Rows[index].DataBoundItem as EntityLocalizationEntry; if (e.CheckFields) { if (comparer(entry.Property, e.Pattern)) { break; } } if (e.CheckValues) { if (comparer(entry.Value, e.Pattern)) { break; } } } if (index < dgv.Rows.Count) { dgv.ClearSelection(); dgv.Rows[index].Selected = true; dgv.FirstDisplayedScrollingRowIndex = index; break; } if (!fromBeginning) { if (MessageBox.Show("Pattern not found. Do you want to start the search from the beginning?", "Search", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { dgv.ClearSelection(); fromBeginning = true; } else { break; } } else { MessageBox.Show("Pattern not found.", "Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); break; } } while (true); }
void frmFind_SearchRequested(object sender, SearchEventArgs e) { var dgv = (sender as FrmSearch).SearchSource ?? dgvCurrent; Func<String, String, bool> comparer; Func<String, String> modifier; if (!e.MatchCase) modifier = a => a.ToLower(); else modifier = a => a; switch (e.SearchCriteria) { case SearchCriteria.Contains: comparer = (a, b) => modifier(a).Contains(modifier(b)); break; case SearchCriteria.StartsWith: default: comparer = (a, b) => modifier(a).StartsWith(modifier(b)); break; } bool fromBeginning = false; do { int index = dgv.SelectedCells.Count > 0 ? dgv.SelectedCells[0].RowIndex : -1; while (++index < dgv.Rows.Count) { var entry = dgv.Rows[index].DataBoundItem as EntityLocalizationEntry; if (e.CheckFields) if (comparer(entry.Property, e.Pattern)) break; if (e.CheckValues) if (comparer(entry.Value, e.Pattern)) break; } if (index < dgv.Rows.Count) { dgv.ClearSelection(); dgv.Rows[index].Selected = true; dgv.FirstDisplayedScrollingRowIndex = index; break; } if (!fromBeginning) { if (MessageBox.Show("Pattern not found. Do you want to start the search from the beginning?", "Search", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { dgv.ClearSelection(); fromBeginning = true; } else break; } else { MessageBox.Show("Pattern not found.", "Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); break; } } while (true); }