Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6923675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:33:56+00:00 2026-05-27T10:33:56+00:00

I am using an ObservableCollection bound to a WPF DataGrid, the collection contains instances

  • 0

I am using an ObservableCollection bound to a WPF DataGrid, the collection contains instances of a custom class (About 500) and the custom class is relativly basic (2 Strings, an IP and a custom enum)

I am using the following code to filter the grid (using all columns) as text is typed into a textbox.

private void searchBox_TextChanged(object sender, TextChangedEventArgs e)
{
    CollectionViewSource SearchView = new CollectionViewSource();
    SearchView.Filter += Search_Filter;
    SearchView.Source = HostList;
    dataGridMain.ItemsSource = SearchView.View;
}

void Search_Filter(object sender, FilterEventArgs e)
{
    if (e.Item != null)
    {
        Host host = e.Item as Host;
        try
        {
            bool foundInHost = false;
            bool foundInIP = false;
            bool foundInUser = false;

            foundInHost = host.Hostname.ToLower().Contains(searchBox.Text.ToLower());
            foundInIP = host.IP.ToString().Contains(searchBox.Text.ToLower());
            foundInUser = host.Username.ToString().Contains(searchBox.Text.ToLower());

            if (foundInHost || foundInIP || foundInUser)
            {
                e.Accepted = true;
            }
            else
            {
                e.Accepted = false;
            }
        }
        catch (Exception ex)
        {

        }
    }
}

It works but even on my shiny new i7 Laptop takes way too long.

Can anyone suggest a quicker method of filtering an observable collection?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:33:57+00:00Added an answer on May 27, 2026 at 10:33 am

    Well all the ToLower and contains stuff isn’t helping, but the most immediate fix would be

    bool found = host.Hostname.ToLower().Contains(searchBox.Text.ToLower());             
    if (!found)
    {
      found = host.IP.ToString().Contains(searchBox.Text.ToLower());
      if (!found)
      {
        found = host.Username.ToString().Contains(searchBox.Text.ToLower()); 
      }
    }
    

    Don’t need the other tests if one of them is already true.

    searchBox.CharacterCasing to Lower would help as well.

    And get rid of that exception swallow as well (the try catch). That’s a really bad habit.
    At the very least add an errorlog to the app, log the exception and then swallow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF DataGrid bound to ObservableCollection . Each item in my collection
using WPF/C#/Entity Framework. I have a DataGrid.ItemsSource bound to some ObservableCollection . Now, I
I have a WPF Toolkit DataGrid bound to an ObservableCollection of Car in my
I have a listbox in WPF that's bound to a ObservableCollection using the ItemSource
Currently I am using an ObservableCollection within a WPF application, the application is an
I have an observable collection bound to a listbox in WPF. One of the
I'm trying to unit test a custom WPF control using NUnit. The control is
Using MVVM style I have successfully bound an ObservableCollection<string> to a ListBox , showing
I'm using the MVVM Light Toolkit and I have a DataGrid bound to an
I have an ObservableCollection Bound to a WPF List View. I am looking to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.