Not sure if this the right place for this question but I am developing a WPF app. I have a listbox and a text box in my XAML code, based on the user input in the text box the Dataset must get filtered and the list box must only show the relevant rows.
For example: if the user types in the aplhabet 'e' in the text box the listbox must only show words like 'elephant', 'egg' etc.
My problem is that when the form loads the listbox gets filled with 1000 words, but when I type anything in the text box, the listbox becomes blank.
I have tried filtering the Dataset itself using
dataset.tables[0].select
and I have also tried using DataView but nothing seems to work. Any pointer to what I am doing wrong.
Best Regards
@nand
Here is the code:
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.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
namespace MediaApp
{
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : System.Windows.Window
{
OleDbConnection oleCon;
OleDbCommand oleComd;
string strSql = "SELECT * FROM media";
DataSet dtst;
OleDbDataAdapter adpt;
public Window1()
{
InitializeComponent();
BindData();
}
public void BindData()
{
oleCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\media.mdb");
oleComd = new OleDbCommand(strSql, oleCon);
dtst = new DataSet();
adpt = new OleDbDataAdapter();
try
{
oleCon.Open();
adpt.SelectCommand = oleComd;
adpt.Fill(dtst, "media");
lstEmployee.DataContext = dtst;
}
catch (Exception ex)
{
}
finally
{
oleCon.Close();
}
}
private void SearchText_KeyDown(object sender, KeyEventArgs e)
{
DataTable dt = dtst.Tables[0] ;
DataView dv = new DataView();
dv = dt.DefaultView;
dv.RowFilter = "mediaid = 5700";
dv.Sort = "mediaid";
dv.RowStateFilter = DataViewRowState.ModifiedCurrent;
listBox1.DataContext = dv;
}
}
}
Anand ShahPosted Jan 5, 2009, 9:39 AM
Many Thanks
@nand
Anand ShahPosted Jan 5, 2009, 3:39 AM
Thanks for the reply
1. Mediaid is an integer field. "mediaid = 5700" is just for the sake of example, once the filtering works I will change the code to filter based on what has been entered in the text box.
2. Yes, there is a record in the database with the mediaid 5700.
Many thanks for your time and efforts.
Best Regards
@nand
Jan MontanoPosted Jan 4, 2009, 8:52 PM
in your code:
dv.RowFilter = "mediaid = 5700";
regardless of what the user types, it will always search for records having a mediaid of value 5700.
2. Do you have a record with mediaid = 5700 ?