Академический Документы
Профессиональный Документы
Культура Документы
A useful feature to add to your form is a Find button. When a find button
is clicked, you then display the record that the user was searching for.
Or display a "Not found" message if there were no matching records.
So add a new button to your form. Set the Text property to Find, and
the Name property to btnFind. Double click your button to get at the
coding window.
There are quite a few different ways you can implement a search. The
method we'll use is to Select a row from the dataset. We'll allow a user
to search using a last name.
string searchFor = "Khan";
int results = 0;
DataRow[] returnedRows;
To get at a particular Row in your Dataset, you can use the Select
method. Here's the code. It's a bit long, so we've had to spread it over
two lines. It should be one line in your code:
returnedRows =
ds1.Tables["Workers"].Select("last_Name='" + searchFor
+ "'");
So you start with your Dataset, which was ds1 for us. Then you need
the name of a Table in your Dataset. We want to search the "Workers"
table. After a dot, we have the Select method:
It looks a bit messy with all those quote marks. But first we have an
outer pair:
Select(" ");
last_Name=
You need to type the name of a Column from your Dataset, here. We're
using the last_Name Column. But we could have used the first_Name
Column instead:
first_Name=
The Column names are the same ones we used in our database table.
But notice the equals sign. The Select method allows you to use other
SQL keywords. If you don't want an exact search, for example, you can
use Like instead of =.
Note where the single quotes are - surrounding the text you want to
search for. Because our search used a variable, we're using plus
symbols to concatenate. Which is why it's so messy!
But here's what your code should look like: (We're using word wrap)
If a row is found, it will then be stored in the returnedRows array. To get
a count of how many rows were found, we can used this code:
results = returnedRows.Length;
if (results > 0)
{
//RECORD FOUND
}
else
{
MessageBox.Show("No such Record");
}
if (results > 0)
{
DataRow dr1;
dr1 = returnedRows[0];
}
Putting it all together, here's the full code for the search:
Notice the line that display the job title in a Message box:
MessageBox.Show( dr1["job_Title"].ToString() );
Because dr1 is now a DataRow, you can access its data by either using
the Column name, or the index number. So these lines return the same
values:
dr1["job_Title"]
dr1["first_Name"]
dr1["last_Name"]
dr1[3]
dr1[1]
dr1[2]
But try your programme out. Click your Find button and the job title of
the person named Khan should appear in the message box.
Close your programme down. Change the name of the person being
searched for and try again.
Exercise
Add a text box to your form. Get the name of the person from this text
box, rather than using the hard coded value that you have at the
moment.
Exercise
Add a drop down list next to the text box. The drop down list should
allow a user to search by last name, or by job title. (Searching by first
name doesn't make much sense - too many people with the same first
name!)
When you have finished these two exercises, your form might look
something like this:
Exercise
As you can see from the form above, although the Supervisor is
displayed in the message box, it's the IT Manager who appears in the
text boxes. For this exercise, display the person's details in the text
boxes rather than in a message box.