【发布时间】:2020-02-15 11:46:25
【问题描述】:
我想实现 Excel 之类的三列过滤数据网格。
这是我的代码:
using System.Data.Odbc;
using System.Windows;
using System.Data;
namespace DB_inspector
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
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);
DataGrid1.DataContext = table;
DataGrid1.ItemsSource = table.DefaultView;
dadapter.Update(table);
dbConnection.Close();
}
private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
DataView dv = DataGrid1.ItemsSource as DataView;
string filter = NameSearch.Text;
if (string.IsNullOrEmpty(filter))
dv.RowFilter = null;
else
dv.RowFilter = string.Format("NAME Like '%{0}%'", filter);
}
private void TextBox_TextChanged_1(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
DataView dv = DataGrid1.ItemsSource as DataView;
string filter = ActiveSearch.Text;
if (string.IsNullOrEmpty(filter))
dv.RowFilter = null;
else
dv.RowFilter = string.Format("ACTIVE Like '%{0}%'", filter);
}
private void CustomerNumberSearch_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
DataView dv = DataGrid1.ItemsSource as DataView;
string filter = CustomerNumberSearch.Text;
if (string.IsNullOrEmpty(filter))
dv.RowFilter = null;
else
dv.RowFilter = string.Format("NRO Like '%{0}%'", filter); //this should be "Begins with" not "Like"
}
}
}
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:DB_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"/>
<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="TextBox_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"/>
<TextBox x:Name="ActiveSearch" HorizontalAlignment="Left" Height="23" Margin="502,14,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="66" TextChanged="TextBox_TextChanged_1"/>
<TextBox x:Name="CustomerNumberSearch" HorizontalAlignment="Left" Height="23" Margin="580,14,0,0" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="82" TextChanged="CustomerNumberSearch_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>
我想为NRO 和ACTIVE 列实现过滤器,以便在加载NRO 列中的数据期间,数字仅以3 开头,而ACTIVE 列中的值仅以1 开头。在ACTIVE 中有值 1 和 0(1 有效,0 无效)。
我当前的设置仅按一个标准进行过滤。如何使所有三个标准功能正确?
【问题讨论】:
-
您是否知道这将排除具有部分有效条件的行?将排除包含
NRO = 3和Active = 0之类的值的行。还是应该将条件与 OR 结合起来?