Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 27th, 2006, 09:31 AM
Authorized User
 
Join Date: Feb 2006
Location: Bluefields, , .
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to MAKO
Default data filtering from combobox

Hello people!

I need to filter the content of an employees table to be displayed on a combobox from the selection of an employer combobox. In other words, the employer combobox gives me the employer id, and I need only the employees that correspond to that employer to displayed. How can I achive that?




MAKO - "El super simio"
__________________
MAKO - \"El super simio\"
Reply With Quote
  #2 (permalink)  
Old May 28th, 2006, 06:39 AM
Authorized User
 
Join Date: May 2004
Location: Karachi, Sindh, Pakistan.
Posts: 83
Thanks: 0
Thanked 1 Time in 1 Post
Default

It seems you have read-in both the Employers table and the Employees table in DataTable objects and wish to filter the Employees table based on the Employer ID from the Employers table the user selects. You can do this easily by creting a DataView object based on the Employees DataTable like this.

Code:
DataView employeeView = new DataView(employeeTable);
Now suppose the Employer ID has changed and you wish to filter employees based on it. Assume that the Employer ID is numeric. You would do it like this:

Code:
// Assume we have ID of the selected Employer in a variable employerID
employeeView.RowFilter = employerID;
This would filter the employeeView DataView so that it would contain only those employees whose id is equal to employerID.

If you've bound a control, a DataGrid or ComboBox for example, to the employeeView, the control would auto-update itself to reflect whatever data is contained in the employeeView. In other words, the bound control would contain the filtered employees (because the DataView has been filtered).

I hope this helps.

ejan
Reply With Quote
  #3 (permalink)  
Old May 31st, 2006, 03:18 PM
Authorized User
 
Join Date: Feb 2006
Location: Bluefields, , .
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to MAKO
Default

Hi ejan, I tried your advise and it seems to work, but what happens if the employer id is a string? I wrote the code but I get an evaluation exception.

MAKO - "El super simio"
Reply With Quote
  #4 (permalink)  
Old June 1st, 2006, 03:52 AM
Authorized User
 
Join Date: May 2004
Location: Karachi, Sindh, Pakistan.
Posts: 83
Thanks: 0
Thanked 1 Time in 1 Post
Default

To filter a DataView using a column whose data type is string, you'd use code similiar to this:

// Retrieve id of employer.
string employerId = "007";

// Filter employees based on employer id.
employeeView.RowFilter = "'" + employerId + "'";

By the way, the DataView.RowFilter property can be set to almost any valid SQL WHERE clause without specifying the WHERE keyword.

ejan
Reply With Quote
  #5 (permalink)  
Old June 1st, 2006, 03:54 AM
Authorized User
 
Join Date: May 2004
Location: Karachi, Sindh, Pakistan.
Posts: 83
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sorry, a little mistake. The second line of code should read like this:

// Filter employees based on employer id.
employeeView.RowFilter = "EmployerId = '" + employerId + "'";

Cheers.


ejan
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filtering combobox from a dataview. Nellemandela C# 3 August 28th, 2008 02:10 PM
Filtering XML data aldwinenriquez XSLT 7 August 25th, 2008 03:24 AM
Filtering Data after its been cached Durkee VB.NET 2002/2003 Basics 0 November 13th, 2007 05:57 PM
Filtering User data donydog Classic ASP Databases 0 February 6th, 2006 09:33 PM
Need Help on Filtering duplicated data fanyin XSLT 2 November 4th, 2004 08:06 PM



All times are GMT -4. The time now is 01:49 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.