Вы можете изменить запрос к базе данных и позволить базе данных выполнять работу более эффективно.
Но если вы все еще предпочитаете делать это с помощью C#, вы можете использовать LINQ для фильтрации DataTable
:
MainWindow.xaml
<Window>
<Window.DataContext>
<ViewModel />
</Window.DataContext>
<DataGrid ItemsSource={Binding Data} />
</Window>
ViewModel.cs
public class ViewModel
{
public DataTable Data { get; set; }
public ViewModel()
{
this.Data = new DataTable();
}
// Button ICommand handler
private void ExecuteGetDataCommand(object param)
{
DataTable dataTable = QueryDatabase();
// Filter DataTable using LINQ
this.Data = dataTable
.AsEnumerable()
.Where(row => row["NRO"].ToString().StartsWith("3")
&& row["ACTIVE"].ToString().StartsWith("1"))
.CopyToDataTable();
}
private DataTable QueryDatabase()
{
OdbcConnection dbConnection = new OdbcConnection("Driver={Pervasive ODBC Client Interface};ServerName=875;dbq=@DBFS;Uid=Username;Pwd=Password;");
string strSql = "select NRO,NAME,NAMEA,NAMEB,ADDRESS,POSTA,POSTN,POSTADR,COMPANYN,COUNTRY,ID,ACTIVE from COMPANY";
dbConnection.Open();
OdbcDataAdapter dadapter = new OdbcDataAdapter();
dadapter.SelectCommand = new OdbcCommand(strSql, dbConnection);
DataTable table = new DataTable("COMPANY");
dadapter.Fill(table);
return table;
}
}
Обновление
Это решение на основе вашего размещенного кода. Я заменил TextBox
, чтобы отфильтровать столбец ACTIVE
на CheckBox
, чтобы добавить безопасность типов (для предотвращения неправильного ввода, например, в алфавитном порядке). Я также использовал привязку данных для подключения DataGrid
к DataTable
. Вы должны предпочесть использовать привязку данных к выделенному коду и реализовать как можно больше в XAML:
MainWindow.xaml
<Window x:Class="DB_inspector.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Liinos_inspector"
mc:Ignorable="d"
Title="DB database inspector" Height="595.404" Width="1005.571">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="11*"/>
<RowDefinition Height="553*"/>
</Grid.RowDefinitions>
<DataGrid x:Name="DataGrid1" Margin="0,51,0,0" Grid.Row="1"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MainWindow}}, Path=FilteredData}" />
<Image Height="41" Margin="847,10,10,502" Width="141" Source="Logo_small.jpg" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Top"/>
<TextBox x:Name="NameSearch" HorizontalAlignment="Left" Height="23" Margin="77,14,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="409" Grid.Row="1"
TextChanged="OnNameSearchTextBox_TextChanged"/>
<Button Content="Refrsh" HorizontalAlignment="Left" Height="23" Margin="736,14,0,0" VerticalAlignment="Top" Width="72" Click="Button_Click" Grid.Row="1" Background="#FF51C951" BorderBrush="{x:Null}" Foreground="White"/>
<CheckBox x:Name="ActiveSearch" HorizontalAlignment="Left" Height="23" Margin="502,14,0,0" Grid.Row="1" VerticalAlignment="Top" Width="66"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MainWindow}}, Path=IsActiveFilterValue}" />
<TextBox x:Name="CustomerNumberSearch" HorizontalAlignment="Left" Height="23" Margin="580,14,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="82"
TextChanged="OnCustomerNumberSearch_TextChanged"/>
<ProgressBar x:Name="ProgressBar" HorizontalAlignment="Left" Height="10" Margin="0,1,0,0" VerticalAlignment="Top" Width="998" BorderBrush="{x:Null}" Background="{x:Null}"/>
<Button Content="Button" HorizontalAlignment="Left" Height="23" Margin="680,14,0,0" Grid.Row="1" VerticalAlignment="Top" Width="51" Click="Button_Click_1"/>
</Grid>
</Window>
MainWindow.xaml .cs
public partial class MainWindow : Window
{
public static readonly DependencyProperty FilteredDataProperty = DependencyProperty.Register(
"FilteredData",
typeof(DataTable),
typeof(MainWindow));
public DataTable FilteredData
{
get { return (DataTable)GetValue(FilteredDataProperty); }
set { SetValue(FilteredDataProperty, value); }
}
public static readonly DependencyProperty IsActiveFilterValueProperty = DependencyProperty.Register(
"IsActiveFilterValue",
typeof(bool),
typeof(MainWindow),
new PropertyMetadata(false, OnIsActiveFilterValueChanged));
public bool IsActiveFilterValue
{
get { return (bool)GetValue(IsActiveFilterValueProperty); }
set { SetValue(IsActiveFilterValueProperty, value); }
}
private DataTable UnfilteredData { get; set; }
private String NameFilterValue { get; set; }
private int CustomerNumberFilterValue { get; set; }
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
string connectionString = "Driver={Pervasive ODBC Client Interface};ServerName=875;dbq=@DBFS;Uid=Username;Pwd=Password;";
string queryString = "select NRO,NAME,NAMEA,NAMEB,ADDRESS,POSTA,POSTN,POSTADR,COMPANYN,COUNTRY,ID,ACTIVE from COMPANY";
// using-statement will cleanly close and dispose unmanaged resources i.e. IDisposable instances
using (OdbcConnection dbConnection = new OdbcConnection(connectionString))
{
dbConnection.Open();
OdbcDataAdapter dadapter = new OdbcDataAdapter();
dadapter.SelectCommand = new OdbcCommand(queryString, dbConnection);
this.UnfilteredData = new DataTable("COMPANY");
dadapter.Fill(this.UnfilteredData);
this.FilteredData = this.UnfilteredData;
}
}
private void ApplyFilterOnDataTable()
{
// Filter DataTable using LINQ
this.FilteredData = this.UnfilteredData
.AsEnumerable()
.Where(row =>
string.IsNullOrWhiteSpace(this.NameFilterValue)
? true // Filter criteria is ignored, when input is empty or null
: row["NAME"].StartsWith(this.NameFilterValue)
&& this.CustomerNumberFilterValue < 0
? true // Filter criteria is ignored, when value is < 0
: row["NRO"].ToString().StartsWith(this.CustomerNumberFilterValue.ToString())
&& this.IsActiveFilterValue
? row["ACTIVE"].ToString().Equals("1")
: true) // Filter criteris is ignored, when CheckBox is unchecked
.CopyToDataTable();
}
private void OnNameSearchTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
this.NameFilterValue = NameSearch.Text;
ApplyFilterOnDataTable();
}
private static void OnIsActiveFilterValueChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var _this = d as MainWindow;
_this.ApplyFilterOnDataTable();
}
private void OnCustomerNumberSearch_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
// Check if input is numeric
this.CustomerNumberFilterValue = Int32.TryParse(CustomerNumberSearch.Text, out int number)
? number
: -1; // Parsing failed -> not a numeric value. -1 will disable this filter criteria
ApplyFilterOnDataTable();
}
}