Microsoft KB Archive/121287

{|
 * width="100%"|

How to View Multiple Records of CRecordset with GRID.VBX

 * }

Q121287

-

The information in this article applies to:


 * The Microsoft Foundation Classes (MFC), included with:
 * Microsoft Visual C++, version 1.5

-

SUMMARY
You can use the GRID.VBX custom control to display data in a series of rows and columns by using various properties of the Grid control and displaying the contents of a CRecordset object in a Grid.

MORE INFORMATION
The sample code below uses the Student table of the "Student Registration" Data Source that is used by the Enroll tutorial in the Database Classes manual. You will need to modify portions of the code for use with your own custom data source.

For more information on using the GRID.VBX custom control, see MFC TechNote #27, the VBChart sample, and query on the following words in the Microsoft Knowledge Base:

GRID.VBX and SAMPLE

Sample Code
void FillGridControl(CVBControl* pGrid, CMyRecordset* pSet) {  #define TWIPS_PER_INCH  1440

// These are the arbitrary widths // of the columns of the recordset in twips #define STUDENTID_WIDTH 1000 #define NAME_WIDTH     2000 #define GRADYEAR_WIDTH 1000

char buf[80];

// calculate number of records in the record set while (!pSet->IsEOF) pSet->MoveNext; int nRows = (int)pSet->GetRecordCount;

// set # of rows and columns for the Grid control pGrid->SetNumProperty("Cols", 4); pGrid->SetNumProperty("Rows", nRows + 1);

// set height and width of grid control CDC* pDC = pGrid->GetParent->GetDC; pGrid->SetNumProperty("Height",        6 * (1 + MulDiv((int)pGrid->GetNumProperty("RowHeight"),         pDC->GetDeviceCaps(LOGPIXELSY), TWIPS_PER_INCH))); pGrid->SetNumProperty("Width",        5 + MulDiv((int)pGrid->GetNumProperty("ColWidth", 0) + STUDENTID_WIDTH + NAME_WIDTH + GRADYEAR_WIDTH, pDC->GetDeviceCaps(LOGPIXELSX), TWIPS_PER_INCH) +        ::GetSystemMetrics(SM_CXVSCROLL)); pGrid->GetParent->ReleaseDC(pDC);

// Initialize column headings pGrid->SetNumProperty("Row", 0);

pGrid->SetNumProperty("Col", 1); pGrid->SetNumProperty("ColWidth", STUDENTID_WIDTH, 1); pGrid->SetStrProperty("Text", "StudentID");

pGrid->SetNumProperty("Col", 2); pGrid->SetNumProperty("ColWidth", NAME_WIDTH, 2); pGrid->SetStrProperty("Text", "Name");

pGrid->SetNumProperty("Col", 3); pGrid->SetNumProperty("ColWidth", GRADYEAR_WIDTH, 3); pGrid->SetStrProperty("Text", "GradYear");

// Initialize row headings pGrid->SetNumProperty("Col", 0); for (int i = 1; i <= nRows; i++) {     wsprintf(buf, "%d", i); pGrid->SetNumProperty("Row", i); pGrid->SetStrProperty("Text", buf); }

// Initialize cell contents pSet->MoveFirst; for (i = 1; i <= nRows; i++) {     pGrid->SetNumProperty("Row", i);

pGrid->SetNumProperty("Col", 1); wsprintf(buf, "%ld", pSet->m_StudentID); pGrid->SetStrProperty("Text", buf);

pGrid->SetNumProperty("Col", 2); pGrid->SetStrProperty("Text", pSet->m_Name);

pGrid->SetNumProperty("Col", 3); wsprintf(buf, "%d", pSet->m_GradYear); pGrid->SetStrProperty("Text", buf);

pSet->MoveNext; }

pSet->MoveFirst; } In this code, pGrid is a pointer to a CVBControl variable associated with the control, and pSet is a pointer to an open CRecordset derived object called CMyRecordset.

Remember to add the EnableVBX function to the InitInstance function of your CWinApp derived class.

You may also want to enable direct editing of the Grid control's fields as demonstrated by the VBChart sample mentioned above.

Additional query words: kbinf 1.50 2.50 multirecord odbc

Keywords : kb16bitonly

Issue type :

Technology : kbAudDeveloper kbMFC