gpt4 book ai didi

c# - 筛选由 MySQL 填充的 WPF DataGrid C#

转载 作者:行者123 更新时间:2023-11-29 21:57:05 24 4
gpt4 key购买 nike

我想在用户在搜索框中键入内容时过滤我的 DataGrid。我通过以下方式将数据绑定(bind)到 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的方法。我怎样才能在我的程序中做到这一点?

最佳答案

首先,您不应该真正将原始数据绑定(bind)到 View 。创建一个代表您的数据的自定义类,并像访问任何其他普通 C# 对象一样访问它。这样您就可以将数据与 View 分开,并接近 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>

关于c# - 筛选由 MySQL 填充的 WPF DataGrid C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33037022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com