|
Free
C# Tutorials
|
![]() |
home |
Stay
at Home and Learn
|
|||||
Database Navigation Buttons |
||||||
|
<< Part of an ongoing lesson - first part is here >> The first thing we'll do is to allow users to move forward through each record in our database. This is done with just a bit of programming logic, and manipulating the Row value in the Dataset. To make this work, we need to set up a few variables. So return to your coding window, and add the following two variables outside the form load event, just below the three you already have: int MaxRows
= 0; Your coding window will then look like this: (The first three lines for SQL Server Express users will be slightly different.)
The MaxRows variable will hold how many Rows there are in the Dataset. This is so that we don't go past the last record when the Next Record button is clicked. If we try to go past the last record, the programme will crash! (We'll add the button shortly.) The inc variable will be used to change the current Row number. To get at the number of Rows in the DataSet, you can use the Count property of Rows. Add this code to your form load event, just below your call to NavigateRecords( ): MaxRows = ds1.Tables["Workers"].Rows.Count; Instead of specifying a particular Row in square brackets, this time
we type a dot, and then select Count from the IntelliSense list. This
will return how many rows there are in this particular Dataset. Here's
what your code should look like
When the form loads, then, MaxRows will contain a count of how many Rows are in the Dataset called ds1. For the NavigateRecords method, we need to make one slight change. At the moment, we have this code: DataRow dRow = ds1.Tables["Workers"].Rows[0]; But this will point to Row[0] all the time. We can use the inc variable here. What we'll do is to increment the value when the Next Record button is clicked, adding 1 to inc every time. Change the line to this: DataRow dRow = ds1.Tables["Workers"].Rows[inc]; The only change is in between the square brackets of Rows. Run your programme to test if it works. You should still see the first record displayed in your text boxes. Stop your programme and return to the design environment. Add a button to your form. Change the Text property to Next Record. Change the Name property to btnNext. Your form will then look like this:
Double click your button to get at the coding window. For the code, we need to check what is inside of the MaxRows variable and make sure we don't go past it. We also need to increment the inc variable. It is this variable that will move us on to the next record. Add the following If Statement to your button
The first line of the If Statement says "If inc does not equal MaxRows minus 1". If it doesn't then we increment the inc variable and call NavigateRecords. But can you see why we need to say MaxRows - 1? It's because of the Rows[inc] line in our NavigateRecords method. The count for Rows starts at zero. So if we only have 4 records in the database, the count will be for 0 to 3. MaxRows, however, will be 4. If we don't deduct 1, the programme will crash with an error: IndexOutOfRange. If the MaxRows is reached, then we can display a message for the user. Run your programme and test it out. You should be able to move forward through your database. Here's what your form should look like when the last record is reached:
In the next lesson, you'll learn how to move backwards through the database.
<-- Displaing Data | Moving to Previous Records --> <--Back to the C# .NET Contents Page View all our Home Study Computer Courses
|