Anand Shah

Anand Shah

  • 0
  • 30
  • 0

Filtering a Dataset

Jan 4 2009 9:32 AM
Hi,
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
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    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;   
        }

    }
}


Answers (3)