Displaying and Navigating Through the Data

   

The data that is contained within a DataTable is in the form of a collection. Because this is the case, you can index or enumerate through the data using familiar collection class methods. You can read Hour 16, "Collections and Arrays," for a review of collections. Therefore, you should create a function that when given an index (more specifically a row index) will fill in the text boxes on the form. Create a private member function that returns a bool and accepts an integer. This integer specifies which row within the DataTable you want to display. Give your function the name FillFormData. Also, so that you can keep track of which row is currently displayed on the form, create a private member variable that is an integer and give it the name m_iCurRow.

The first thing you should do within the FillFormData function is to check to make sure the row index that is passed in does not extend past the count of the number of rows within the DataTable. Also, you should make sure the row index is not less than zero, because negative row numbers are obviously undefined. This error checking can be seen on line 3 of Listing 20.5. If the conditional error checking passes, you can display the data on the form. Save the row index into your current row index member variable. Next, you will need to navigate to the row within the DataTable collection. Again, this is accomplished by getting a pointer to the Rows collection using the get_Rows member function contained within the DataTable object. This will give you the collection of all the rows within that table, but you are interested in only one of the rows, as specified by the row index passed in as a parameter. Call the get_Item member function contained within the Rows collection, as shown on line 8 of Listing 20.5.

Now that you have navigated to the correct row, you can set the Text properties of the individual TextBox controls to their corresponding columns contained within the Row collection. Because calling get_Item on the collection of data returns a pointer to a .NET Object, convert that value to a string with the ToString function.

Finally, call the FillFormData function within your form class's constructor, passing it 0 as the row index you wish to view. Call this function after the call to initialize the Windows Form. In other words, the FillFormData function call should be the last function called from within the constructor.

Listing 20.5 Filling Form Controls with Data from the DataTable Object
 1: bool AuthorDBForm::FillFormData( int iDBRow )  2: {  3:    if( m_pAuthorTable->get_Rows()->Count-1 < iDBRow || iDBRow < 0 )  4:         return false;  5:  6:     m_iCurRow = iDBRow;  7:  8:     DataRow* pRow = m_pAuthorTable->get_Rows()->get_Item(iDBRow);  9: 10:     textBoxID->Text   = pRow->get_Item("AU_ID")->ToString(); 11:     textBoxName->Text = pRow->get_Item("Author")->ToString(); 12:     textBoxYear->Text = pRow->get_Item("YearBorn")->ToString(); 13: 14:     return true; 15: } 

At this point, your application will display the first record within the database. Now you are going to implement the code that navigates through the database using the form's navigation buttons. Navigating to the first record in the database is a trivial matter. You already do this in the form constructor, so call the FillFormData function with a row index of 0 in the buttonFirst_Click method. To navigate to the last row, you can use the Count property contained within the Rows collection. You will need to subtract 1 from the value received because row indexes start at 0. This can be seen on line 18 of Listing 20.6. Because you are keeping track of the row you are currently on, you can just call the FillFormData member function with one row index greater or one index smaller than the current row. This allows you to go forward or backward an index at a time.

Listing 20.6 Navigating Through the Data and Updating the Form Controls
 1: void AuthorDBForm::buttonFirst_Click(Object* sender, EventArgs* e)  2: {  3:     FillFormData(0);  4: }  5:  6: void AuthorDBForm::buttonPrevious_Click(Object* sender, EventArgs* e)  7: {  8:     FillFormData( m_iCurRow - 1 );  9: } 10: 11: void AuthorDBForm::buttonNext_Click(Object* sender, EventArgs* e) 12: { 13:     FillFormData( m_iCurRow + 1 ); 14: } 15: 16: void AuthorDBForm::buttonLast_Click(Object* sender, EventArgs* e) 17: { 18:     FillFormData( m_pAuthorTable->get_Rows()->Count -1); 19: } 

Compile and run your application. If everything is working correctly, you should be able to navigate through the records in the DataTable using the form's navigation buttons. Figure 20.4 shows the application displaying the last record in the Authors database.

Figure 20.4. The AuthorDB application displaying the last record in the Authors database.

graphics/20fig04.jpg


   
Top


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net