Building the parameterized search forms

At this point, you have all the functions you need to search for data and to retrieve it as business objects. You will now need to build two screens—one to let the user specify their search parameters and another to display the search results. Let's move on to the final presentation tier.

Building the search form

The parameterized search form allows the user to search using a combination of the following fields:

• Account first name

• Account last name

• Account status

• An option to include any of the three account types in the search—leads, opportunities, and customers

This form lets the user key in his or her search parameters and then saves it in an AccountSearchParameters object. This object can thereafter be easily passed around to the other tiers for processing.

To begin, create a new form called searchAccount and design it based on the following screenshot:

You would probably also want to add a Bindingsource control to the form to bind the controls on the form to your AccountSearchParameters object. You should place the binding code in the form load event.

private AccountSearchParameters _AccountParams = new AccountSearchParameters();

public void SearchAccounts_Load(System.Object sender, System.EventArgs e)

SearchAccountsBindingSource.DataSource = typeof(AccountSearchParameters);

SearchAccountsBindingSource.Add(_AccountParams); txtFirstName.DataBindings.Add(new Binding("Text", SearchAccountsBindingSource, "FirstName", true)); txtLastName.DataBindings.Add(new Binding("Text", SearchAccountsBindingSource, "LastName", true)); cbStatus.DataBindings.Add(new Binding("SelectedIndex",

SearchAccountsBindingSource, "Status", true));

The Search button simply signals to the binding control that editing is done. It then passes the entire AccountSearchParameters object to the next screen, which is the search results listing form.

public void btnSearch_Click(System.Object sender, System.EventArgs e) {

SearchAccountsBindingSource.EndEdit(); NavigationService.ShowDialog("AccountsSearchSummary", ((object) _AccountParams));

The code to launch the search results listing form can be added to the ShowDialog() function in the NavigationService class. Take special note that you need to pass the AccountsSearchParameters object to the search results listing form (following the code highlighted):

public static DialogResult ShowDialog(string DialogName, object Argl)

DialogResult _dialogResult = 0;

switch (DialogName) {

case "AccountsSearchSummary":

AccountsSearchSummary _AccountsSearch = new

AccountsSearchSummary(); _AccountsSearch.SearchParameters = (AccountSearchParameters) Argl; _AccountsSearch.SetupNavigator(10); _dialogResult = _AccountsSearch.ShowDialog(); _AccountsSearch.Close(); _AccountsSearch.Dispose(); _AccountsSearch = null; break;

Building the search results listing form

You can reuse the paging control you've built in the previous chapter to browse through pages of search results. The search results listing form is similar to the AccountsListing form you've built in the last chapter. You can reuse the form design and code from that form.

The only difference is that you need to pass the search parameters to this form. You can easily achieve this via an AccountSearchParameters object. You will also need to change the SetupNavigator() and RefreshPage() functions (highlighted) to call the search methods you've created earlier.

private AccountSearchParameters _SearchParameters;

public AccountSearchParameters SearchParameters {

get {return _SearchParameters;} set {_SearchParameters = value;}

public void SetupNavigator(int RecordsPerPage) {

_recordsPerPage = RecordsPerPage;

_totalRecords = GlobalArea.Application.GetAccountsCountByParameters (_SearchParameters);

pgPager.SetupPager(_totalRecords, RecordsPerPage); RefreshPage();

private void RefreshPage() {

_accounts = GlobalArea.Application.GetAccountsByParameters (_SearchParameters, _totalRecords, pgPager.CurrentPage, _recordsPerPage, _SortColumn, _SortOrder);

dgAccounts.DataSource = _accounts; dgAccounts.Refresh();

Trying out your code

And that's it! You've built your first parameterized search function. Before you can try your code, you will need to create a new icon in the main menu and a corresponding entry in the NavigationService class to launch the SearchAccount form. I'll leave that as an exercise for you.

In the next section, we will move on to an overview of full text search functionality and how to implement it.

Was this article helpful?

0 0

Post a comment