【问题标题】:Filter WPF DataGrid C# Populated by MySQL过滤由 MySQL 填充的 WPF DataGrid C#
【发布时间】:2015-10-09 11:22:57
【问题描述】:

当用户在搜索框中输入内容时,我想过滤我的 DataGrid。我通过以下方式将我的数据绑定到我的 DataGrid;

在我的数据访问器中;

    public DataTable FillDataGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["dbfString"].ConnectionString;
        using (OleDbConnection dbfCon = new OleDbConnection(constr))
        {
            try
            {
                dbfCon.Open();
                DataTable dTable = new DataTable();
                string dbfQuery = "SELECT em_pplid, em_name, em_netname, em_init, em_dept FROM employs WHERE em_netname NOT LIKE ''";
                OleDbCommand MyQuery = new OleDbCommand(dbfQuery, dbfCon);
                OleDbDataAdapter DA = new OleDbDataAdapter(MyQuery);
                DA.Fill(dTable);
                return dTable;
            }
            catch (OleDbException)
            {
                throw;
            }
        }
    }

在我看来;

    private void FillDataGrid(object sender, RoutedEventArgs e)
    {
        DataAccessor da = new DataAccessor();
        DataTable dt = da.FillDataGrid();
        dataGrid.ItemsSource = dt.AsDataView();
    }

在我看来,当我知道如何正确过滤 DataGrid 时,我目前已经准备好成为搜索功能;

    private void SearchGrid(object sender, TextChangedEventArgs e)
    {
        if (nNameRad.IsChecked == true)
        {
            MessageBox.Show("Hello!");
        }
    }

我在网上搜索了很多看似过时的过滤DataGrid的方法。如何在我的程序中执行此操作?

【问题讨论】:

    标签: c# mysql wpf datagrid


    【解决方案1】:

    首先,您不应该真正将原始数据绑定到视图。创建一个代表您的数据的自定义类,并像访问任何其他普通 C# 对象一样访问它。这样,您可以将数据与视图分开,并采用 MVVM 模式。例如,您的数据类可以提供过滤器方法。除了数据类之外,没有其他人应该真正知道如何过滤自己。

    完成此操作后,过滤 ListBox、DataGrid 或您碰巧使用的任何 ItemControl,就变得非常简单。这是一个基本的简单代码。提供您自己的方式来获取数据而不是静态构造函数,这样您就有了可以进一步处理的东西。

    MyFirstData.cs

    public class MyFirstData
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public bool Filter(string filterText)
        {
            return Id.ToString() == filterText || Name.Contains(filterText);
        }
    }
    

    MainWindow.cs

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
    
            Items = new ObservableCollection<MyFirstData>()
            {
                new MyFirstData() {Id = 1, Name = "foo"},
                new MyFirstData() {Id = 2, Name = "bar"},
                new MyFirstData() {Id = 3, Name = "marble"},
            };
            ItemsView = CollectionViewSource.GetDefaultView(Items);
    
            DataContext = this;
        }
    
        private string _filterText;
    
        public string FilterText
        {
            get { return _filterText; }
            set
            {
                _filterText = value;
                ItemsView.Filter = DoFilter;
                OnPropertyChanged();
            }
        }
    
        private bool DoFilter(object obj)
        {
            var myFirstData = obj as MyFirstData;
            if (myFirstData != null)
            {
                return myFirstData.Filter(FilterText);
            }
            return false;
        }
    
        public ObservableCollection<MyFirstData> Items { get; set; }
        public ICollectionView ItemsView { get; set; }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    MainWindow.xaml

    <Window x:Class="FilterTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:FilterTest"
            Title="MainWindow" Height="350" Width="525">
        <DockPanel>
            <TextBox DockPanel.Dock="Top" Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}"/>
            <ListBox ItemsSource="{Binding ItemsView}">
                <ListBox.ItemTemplate>
                    <DataTemplate DataType="local:MyFirstData">
                        <TextBlock>
                            <Run Text="{Binding Id}"/>
                            <Run Text=", "/>
                            <Run Text="{Binding Name}"/>
                        </TextBlock>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </DockPanel>
    </Window>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      相关资源
      最近更新 更多