06CRecordView class

xiaoxiao2021-03-06  63

CRecordView class

CRecordView is a derived class of CFormView that provides a form view (see 6.4.1) to display the current record. A typical recording view is shown in Figure 10.3, the user can display the current record by a form view. By recording view, you can modify, add, and delete data. Users generally need to create a CRecordView's derived class and add controls in their corresponding dialog template.

Figure 10.3 Typical recording view

The record view exchanges data between controls and records in the form using the DDX data exchange mechanism. DDX described earlier is exchanged data between the data members of the control and control parent window, and the record view is exchanged between the control and an external object (CRecordset derived class object). Listing 10.3 shows a DODATAEXCHANGE function of a CRecordView class. The reader can see that the function is to exchange data with the domain data member of the record set object pointing to the m_pset pointer, and the code of the exchange data is ClassWizard to be added. In the following example, the reader will introduce the method of connecting the record view and the record set object with the ClassWizard connection to the reader.

Listing 10.3 DODATAEXCHANGE functions for exchange data with domain data members of recorded objects

void CSectionForm :: DoDataExchange (CDataExchange * pDX) {CRecordView :: DoDataExchange (pDX); // {{AFX_DATA_MAP (CSectionForm) DDX_FieldText (pDX, IDC_COURSE, m_pSet-> m_CourseID, m_pSet); DDX_FieldText (pDX, IDC_SECTION, m_pSet-> m_SectionNo, m_pSet); DDX_FieldText (pDX, IDC_INSTRUCTOR, m_pSet-> m_InstructorID, m_pSet); DDX_FieldText (pDX, IDC_ROOM, m_pSet-> m_RoomNo, m_pSet); DDX_FieldText (pDX, IDC_SCHEDULE, m_pSet-> m_Schedule, m_pSet); DDX_FieldText (pDX , IDC_capacity, m_pset-> m_capacity, m_pset); //}} AFX_DATA_MAP}

As a summary, Figure 10.4 shows DDX and RFX data exchange in the ODBC application of the MFC.

Figure 10.4 DDX and RFX Data Exchange Mechanism

CRecordView itself provides support for the following four commands:

ID_Record_First // Scroll to the first record of the recordset

ID_Record_Last // Scroll to the last record of the recordset

ID_Record_Next // Before entering a record

ID_Record_PREV // Back a record

CRecordView provides the ONMOVE member function to handle these four command messages, and the onmove function is transparent to the user, and Listing 10.4 lists the source code for the OnMove.

Listing 10.4 Onmove function

Bool CRecordView :: OnMove (uint nidmoveCommand) {crecordset * pset = OnGetRecordSet (); if (pset-> canupdate ()) {pset-> edit (); if (! Updatedata ()) Return True;

Pset-> Update ();

Switch (NidmoveCommand) {copy ID_record_prev: pset-> moveprev (); if (! pset-> isbof ()) Break; Case ID_Record_First: Pset-> MoveFirst (); Break;

Case ID_record_next: pset-> movelnext (); if (! pset-> ity) Break; if (! pset-> canscroll ()) {// clear out screen since We're Sitting on Eof Pset-> setfieldnull NULL); BREAK;

Case ID_record_last: pset-> movest (); Break;

DEFAULT: // Unexpected Case Value Assert (false);

// Show results of move operation updata (false); return true;}

In the beginning of the function, call CRecordset :: Edit to edit mode, then call Updatedata to update the data in the control to the domain data member of the recordset object, and then call CRecordset :: Update to write the value of the domain data into the data source. This shows that OnMove will complete the modification of the original record while scrolling records.

There is a branch statement in the middle of the function to handle four different commands, call the various members of CRecordset for scrolling records in this branch statement, which will record the record when scrolling into a new record The content is set to domain data members. Call UpdateData (false) at the end of the function to set the new current recorded content to the control of the form.

It can be seen that OnMove completed the data exchange process of the two form controls and data sources. By analyzing the function, the reader can learn how to control DDX and DFX data exchange when browsing the record.

转载请注明原文地址:https://www.9cbs.com/read-84774.html

New Post(0)