I have implemented a simple search function that highlights a row in a DataGrid based on a search query. The gist of it is shown below:
public bool scrollToSearch(string query) {
dataGrid.SelectedItems.Clear();
for (; searchIndex < registrants.Count; searchIndex++) {
foreach (string field in registrants[searchIndex]) {
if (field.ToLower().Contains(query)) {
dataGrid.SelectedItem = registrants[searchIndex];
dataGrid.ScrollIntoView(registrants[searchIndex]);
searchIndex++;
return true;
}
}
}
}
It searches the list for a match, then highlights(selects) that row and scrolls it into view. The problem is that, when the DataGrid is sorted, the search will highlight a seemingly random row instead of the first result, because it is searching the original, unsorted list. Is there a way I can get at the sorted list to search it instead?
First do this….
then use
viewin place ofregistrantsin your code.