Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filtering a listbox 2

Status
Not open for further replies.

bouwob

Programmer
Joined
Apr 12, 2006
Messages
171
Location
US
I am creating a windows form object that will allow me to filter on the original set.

I have 2 datasets

dsOriginal //the original data, used so I can pull information many times and not be forced to call the db over and over again
dsNew //my filtered dataset

my function so far
Code:
            DataTable dtStuff = (DataTable)dsDataNew.Tables[0];//pull the datatable out of the dataset
            DataView dvStuff = new DataView(dtStuff);//create a datavuiew
            dvStuff.RowFilter = String.Format("{0} LIKE '%{1}%'", this.strSPText, textBox1.Text.Trim());//set the filter up
            DataTable dt = dvStuff.Table.Clone();//copy the view back to a datatable
            this.dsDataNew.Tables.Clear(); //clear the dataset
            this.dsDataNew.Tables.Add(dt);//add the new datatable into the dataset
            load_data();//load the data

Code:
        private void load_data()
        {
            this.lbItems.Items.Clear();
            foreach (DataRow dr in dsDataNew.Tables[0].Rows)
            {
                this.lbItems.Items.Add(dr[this.strSPText].ToString());
            }
        }

Unfortunately if I run the above with textBox1.Text = "tes"; it should return 10 results but continues to return 0.

Does anybody see anything obvious that I am missing??

tia
 
what version of VS are you using?

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
you'll need to iterate over your rows, checking each for the search condition, and add them to your subset when they do.

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Whats the point of this line then????

dvStuff.RowFilter
 
Try setting the DataView RowState's value to OriginalRows.
I noticed that with the DataView, you want to return the filtered records as a new table object.
Code:
DataTable dt = dvStuff.Table.Clone();//copy the view back to a datatable
This may not work as you'd expect because here you are actually cloning the original table with all the records, ignoring the filter. I think what you intend is...
Code:
DataTable dt = dvStuff.[b]ToTable()[/b];//copy the view back to
From MSDN...
Creates and returns a new DataTable based on rows in an existing DataView.
Hope this helps [wink]
 
[cheers] phinoppix, I didn't catch that. I didn't even know about the DataView object :-(

Now hopefully I remember it when I need it!

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top