WPF数据绑定数据网格过滤/搜索

时间:2010-07-16 12:34:25

标签: wpf database datagrid itemsource

我有一个WPF应用程序,它使用(当前)本地数据库作为绑定源。使用Visual Studio 2010工具,我有一个LINQ-SQL模型,它充当大多数表单的Datacontext。

我所拥有的是带有TextBox和Datagrid的UserControl。使用表在UserControl.Loaded事件上设置datagrid ItemSource。 TextBox分配了一个事件,以便在文本更改并在datagrid上更新ItemSource时对数据库执行查询。

这个问题是查询数据库所花费的时间。因为我正在为每次搜索重新分配DataGrid项目源。

  1. 我应该在UserControl加载时加载所有记录 - 有没有办法在BackgroundWorker或类似工具中异步加载记录?

  2. 我是否需要在每次搜索后重新分配DataGrid ItemsSource,或者是更有效的过滤数据的方式?

  3. 感谢。 利安

    <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>
    

    代码:

    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 个答案:

答案 0 :(得分:5)

我认为你应该在开始时加载数据库中的所有记录 然后在ItemsSource上使用ICollectionView.Filter。 那么你就不必进行数据库交易了

你应该做那样的事情

    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;           
    }

希望这有帮助, 尼达尔。