Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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\"
  #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
  #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"
  #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
  #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


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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.