Je lie déjà toutes les données dans ma grille de données. Maintenant, je veux effectuer une recherche.recherche sur la grille de données wpf
Je l'ai compris, je peux faire une connexion SQL pour rechercher. mais je veux seulement faire une recherche sur mon DataGrid. parce que mes données se lient déjà à mon DataGrid.
est ici le pic
en haut, j'ai une zone de texte. comment puis-je faire une recherche/filtrage en utilisant la zone de texte? peut-être que je pourrais taper «Bahan Baku», il va chercher sur «Nama Kategori». ou je peux aussi taper 'Sayur', il va chercher 'Keterangan Kategori'. et ce que je tape, affectera sur mon DataGrid.
un conseil? Merci.
EDIT
Voici mon code.
public partial class MainWindow : Window
{
//private ICollectionView MyData;
//string SearchText = string.Empty;
ObservableCollection<Category> _data = new ObservableCollection<Category>();
public ObservableCollection<Category> data { get { return _data; } }
public MainWindow()
{
InitializeComponent();
showData();
}
private void showData()
{
OleDbConnection conn = null;
OleDbCommand cmd = null;
try
{
conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbDemo1.accdb");
cmd = new OleDbCommand("select categoryID, categoryDesc, categoryItem from t_category", conn);
conn.Open();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
data.Add(new Category
{
nCateogryID = int.Parse(reader[0].ToString()),
sCategoryDesc = reader[1].ToString(),
sCategoryItems = reader[2].ToString()
}
);
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
conn.Close();
}
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox t = sender as TextBox;
SearchText = t.Text.ToString();
MyData.Filter = FilterData;
}
private bool FilterData(object item)
{
var value = (Category)item;
if (value == null || value.nCateogryID == null)
return false;
return value.sCategoryDesc.ToLower().StartsWith(SearchText.ToLower()) || value.sCategoryItems.ToLower().StartsWith(SearchText.ToLower());
//return value.Book_Id.ToLower().StartsWith(SearchText.ToLower()) || value.Book_Name.ToLower().StartsWith(SearchText.ToLower()) || value.Author_Name.ToLower().ToString().StartsWith(SearchText.ToLower()) || value.Publisher_Name.ToLower().ToString().StartsWith(SearchText.ToLower().ToString()) || value.Book_Genre.ToLower().ToString().StartsWith(SearchText.ToLower().ToString()) || value.Language.ToLower().ToString().StartsWith(SearchText.ToLower().ToString()) || value.Book_EntryDate.ToLower().ToString().StartsWith(SearchText.ToLower().ToString());
}
}
c'est ma catégorie de classe ressemble à:
public class Category
{
public int nCateogryID { get; set; }
public int nCategoryMaster { get; set; }
public int nCategoryDetail { get; set; }
public string sCategoryDesc { get; set; }
public string sCategoryItems { get; set; }
}
et voici mon XAML
<Window x:Class="SearchGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="258" HorizontalAlignment="Left" Margin="12,0,0,12" Name="dataGrid1" VerticalAlignment="Bottom" Width="479">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ncategoryID}" Header="No." IsReadOnly="True" Width="30" />
<DataGridTextColumn Binding="{Binding sCategoryDesc}" Header="Nama Kategori" IsReadOnly="True" Width="160" />
<DataGridTextColumn Binding="{Binding sCategoryItems}" Header="Keterangan Kategori" IsReadOnly="True" Width="247" />
</DataGrid.Columns>
</DataGrid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="479" TextChanged="textBox1_TextChanged" />
</Grid>
</Window>
Je suis cette erreur:
Object reference not set to an instance of an object.
salut .. je peux déjà implémenter votre code .. je peux l'utiliser .. merci btw .. :) mais comment puis-je lier selecteditem de mon datagrid à la zone de texte? une idée? –
signifie que vous voulez lier un élément selected de dataagrid à textbox ou un élément de selecteditem? –
Vous pouvez utiliser Return_IssueBook book = new Return_IssueBook(); book = (Return_IssueBook) datagrid1.CurrentCell.Item; –