// The following is the main code
Void creaddbdlg: onread ()
{
// Todo: add your control notification handler code here
Cdatabase database;
Cstring sqlstring;
Cstring scatid, scategory;
Cstring sdriver = "Microsoft Access Driver (*. mdb )";
Cstring sdsn;
Cstring sfile = "C: // works // readdb // test. mdb"; // change path here
Int IREC = 0;
// Create ODBC connection dinamically
Sdsn. Format ("ODBC; driver = {% s}; DSN =''; DBQ = % s ", sdriver, sfile );
Try
{
// Open the database
Database. Open (null, false, false, sdsn );
// Allocate the recordset
Crecordset recset (& database );
// Build the SQL statement
Sqlstring = "select catid, category"
"From categories ";
// Execute the query
Recset. Open (crecordset: forwardonly, sqlstring, crecordset: readonly );
Resetlistcontrol ();
// Column width and heading
M_listcontrol.insertcolumn (0, "Cat ID", lvcfmt_left,-1, 0 );
M_listcontrol.insertcolumn (1, "category", lvcfmt_left,-1, 1 );
M_listcontrol.setcolumnwidth (0, 70 );
M_listcontrol.setcolumnwidth (1,200 );
// Loop through each record
While (! Recset. iseof ())
{
// Copy each column into a variable
Recset. getfieldvalue ("catid", scatid );
Recset. getfieldvalue ("category", scategory );
// Insert values into the List Control
IREC = m_listcontrol.insertitem (0, scatid, 0 );
M_listcontrol.setitemtext (0, 1, scategory );
// Goto next record
Recset. movenext ();
}
// Set column width
// M_listcontrol.setcolumnwidth (0, lvscw_autosize );
// M_listcontrol.setcolumnwidth (1, lvscw_autosize );
// Close the database
Database. Close ();
}
Catch (cdbexception, E)
{
// If a database exception occured, Show ERROR msg
Afxmessagebox ("Database Error:" + E-> m_strerror );
}
End_catch;
}
Void creaddbdlg: resetlistcontrol ()
{
M_listcontrol.deleteallitems ();
Int inbrofcolumns;
Cheaderctrl * pheader = (cheaderctrl *) m_listcontrol.getdlgitem (0 );
If (pheader)
{
Inbrofcolumns = pheader-> getitemcount ();
}
For (INT I = inbrofcolumns; I> = 0; I --)
{
M_listcontrol.deletecolumn (I );
}
}