데이터그리드에 바인딩되어져있는데이터를 검색 합니다.
CollectionViewSource에 데이터를 담아두고
ICollectionView _customerView = CollectionViewSource.GetDefaultView(__CollectionViewSource.View);
_customerView.Filter = CustomerFilter;
필터 검색을 실행한다.
엔티티
데이터서비스
http://localhost:56323/WcfDataService1.svc
namespace WcfRestService1
{
public class WcfDataService1 : DataService<NorthwindEntities>
{
public WcfDataService1()
{
}
// 이 메서드는 서비스 전반적인 정책을 초기화하는 데 한 번만 호출됩니다.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: 규칙을 설정하여 어떤 엔터티 집합과 서비스 작업을 표시할 수 있고 업데이트할 수 있는지 등을 표시합니다.
// 예제:
config.SetEntitySetAccessRule("*", EntitySetRights.All);
// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
//데이터베이스에서 자세한 오류가 반환되는지 여부
config.UseVerboseErrors = true;
}
}
}
MainWindow.xaml
<Window
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:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:local="clr-namespace:테스트용" mc:Ignorable="d" x:Class="테스트용.MainWindow"
Title="MainWindow" Height="350" Width="525" xmlns:my="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon">
<Window.Resources>
</Window.Resources>
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3A3A3" Offset="0.009"/>
<GradientStop Color="#FF5E5E5E" Offset="0.997"/>
<GradientStop Color="#FF2B2B2B" Offset="0.519"/>
</LinearGradientBrush>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<DataGrid Height="230" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" />
<Button Content="검색" Height="23" HorizontalAlignment="Left" Margin="416,248,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<TextBox Height="22" HorizontalAlignment="Left" Margin="290,249,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Label Content="CompanyName" Margin="191.933,247,223,0" Name="label1" VerticalAlignment="Top" Foreground="White" d:LayoutOverrides="Width" />
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
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 System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Reporting.WinForms;
using System.Net;
using System.IO;
using System.Data.Services.Client;
using System.ComponentModel;
namespace 테스트용
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
CollectionViewSource __CollectionViewSource = null;
//엔티티 만들기
ServiceReference1.NorthwindEntities __Context = new ServiceReference1.NorthwindEntities(new Uri("http://localhost:56323/WcfDataService1.svc"));
public MainWindow()
{
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
//Customers 데이터를 CollectionViewSource 담아준다.
//
var query = from c in __Context.Customers
select c;
__CollectionViewSource = new CollectionViewSource();
__CollectionViewSource.Source = query.ToList();
this.dataGrid1.ItemsSource = __CollectionViewSource.View;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
ICollectionView _customerView = CollectionViewSource.GetDefaultView(__CollectionViewSource.View);
_customerView.Filter = CustomerFilter;
}
//검색 메소드
private bool CustomerFilter(object item)
{
string searchKeyworld = this.textBox1.Text;
ServiceReference1.Customers customer = item as ServiceReference1.Customers;
return customer.CompanyName.Contains(searchKeyworld);
}
}
}