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

  • Home
  • SEARCH
  • 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 1011125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:11:15+00:00 2026-05-16T09:11:15+00:00

I have a WPF application which uses a (currently) local database to act as

  • 0

I have a WPF application which uses a (currently) local database to act as a binding source. Using the Visual Studio 2010 tools I have a LINQ-SQL model which acts as the Datacontext for most forms.

What I have is a UserControl with a TextBox and Datagrid. The datagrid ItemSource is set upon the UserControl.Loaded event with a table. The TextBox has an event assigned so that a query is executed upon the database when the text changes and the ItemSource is updated upon the datagrid.

The problem with this is the time it takes to query the database. As I’m reassigning the DataGrid itemsource for each search.

  1. Should I be loading all records upon the UserControl Loading – is there a way to load records asynchronously in a BackgroundWorker or similar?

  2. Do I need to reassign the DataGrid ItemsSource after each search or is their a more efficient way to filter the data?

Thanks.
Liam

<UserControl x:Class="Tracker.DocumentsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Margin="12,34,12,50" Name="dataGrid1">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=ID}" Header="ID" />
                <DataGridTextColumn Binding="{Binding Path=Reference}" Header="Reference" />
                <DataGridTextColumn Binding="{Binding Path=Subject}" Header="Subject" />
            </DataGrid.Columns>
        </DataGrid>

        <TextBox HorizontalAlignment="Left" Margin="64,8,0,0" Name="txtSearchBox" VerticalAlignment="Top" Width="224" TextChanged="txtSearchBox_TextChanged" />
        <TextBlock Text="Search"  HorizontalAlignment="Left" Margin="11,12,0,0" Name="label1" VerticalAlignment="Top" Height="23" />
    </Grid>
</UserControl>

Code:

using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Tracker.Model;

namespace Tracker
{
    /// <summary>
    /// Interaction logic for DocumentsView.xaml
    /// </summary>
    public partial class DocumentsView : UserControl
    {
        private TrackerDataContext db;

        public DocumentsView()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(DocumentsView_Loaded);
        }

        void DocumentsView_Loaded(object sender, RoutedEventArgs e)
        {
            db = new TrackerDataContext();
            dataGrid1.ItemsSource = db.Documents;
        }

        private void txtSearchBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textbox = sender as TextBox;
            if (textbox != null)
            {
                string searchstr = textbox.Text;
                if (!string.IsNullOrEmpty(searchstr))
                {
                    var filtered = from document in db.Documents
                                   where document.Subject.Contains(searchstr)
                                       || document.Reference.Contains(searchstr)
                                   select document;

                    dataGrid1.ItemsSource = filtered;
                }
                else
                {
                    dataGrid1.ItemsSource = db.Documents;
                }
            }
        }
    }
}
  • 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-16T09:11:15+00:00Added an answer on May 16, 2026 at 9:11 am

    I think you should load all the record from the database at the begining
    and then use ICollectionView.Filter on the ItemsSource.
    So then you will not have to do data base transaction

    You should wrtie something like that

        private void txtSearchBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textbox = sender as TextBox;
            if (textbox != null)
            {
                _searchstr = textbox.Text;
                if (!string.IsNullOrEmpty(_searchstr))
                {
                    ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
                    view.Filter = new Predicate<object>(filter);
                }
            }
        }   
    
        private bool filter(object item)
        {
            if(item.Subject.Contains(_searchstr) || item.Reference.Contains(searchstr))
            {
                return true;
            }
            return false;           
        }
    

    Hope this helps,
    Nidal.

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

Sidebar

Related Questions

I have am using a WPF application which uses BitmapSource but I need to
I have a C# WPF application which uses Excel interop libraries to generate and
I have a WPF application which connects to a remote database over internet and
I have a C# application of which some parts are written using WPF (which
I have a WPF application which uses a JumpList (Recent only). Everything works perfectly
I have a client application (WPF, C#, .net4) which uses POCO entity model connected
I have an WPF application which has a local rdlc report which show receipt
I have a WPF application which uses a WPF user control. The user control
I have a C# WPF desktop application which uses SQL Compact 3.5 as its
I have a WPF application which uses some library code for authentication which needs

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.