home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Basic / GridOne / setup.EXE / ADODLG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-09  |  4.2 KB  |  152 lines

  1. // BinaryDataDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "ADO.h"
  6. #include "ADODlg.h"
  7.  
  8. // Change path to match your configuration
  9. #import "D:\Program Files\Common Files\System\ado\msado15.dll" rename("EOF","Eof") rename("BOF","Bof") 
  10.  
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18.  
  19. LPCTSTR g_sampleDescription = 
  20.    _T("This sample demonstrates how to bind the BeeGrid control to an ADO recordset.")
  21.    _T("");
  22.  
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CADODlg dialog
  26.  
  27. CADODlg::CADODlg(CWnd* pParent /*=NULL*/)
  28.     : CDialog(CADODlg::IDD, pParent)
  29. {
  30.     //{{AFX_DATA_INIT(CADODlg)
  31.     m_description = g_sampleDescription;
  32.     //}}AFX_DATA_INIT
  33.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  34.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  35. }
  36.  
  37. void CADODlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39.     CDialog::DoDataExchange(pDX);
  40.     //{{AFX_DATA_MAP(CADODlg)
  41.     DDX_Text(pDX, IDC_DESCR, m_description);
  42.     //}}AFX_DATA_MAP
  43.    DDX_BeeGrid(pDX, IDC_GRID1, m_grid);
  44. }
  45.  
  46. BEGIN_MESSAGE_MAP(CADODlg, CDialog)
  47.     //{{AFX_MSG_MAP(CADODlg)
  48.     ON_WM_PAINT()
  49.     ON_WM_QUERYDRAGICON()
  50.     //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP()
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CADODlg message handlers
  55.  
  56. BOOL CADODlg::OnInitDialog()
  57. {
  58.     CDialog::OnInitDialog();
  59.  
  60.     // Set the icon for this dialog.  The framework does this automatically
  61.     //  when the application's main window is not a dialog
  62.     SetIcon(m_hIcon, TRUE);            // Set big icon
  63.     SetIcon(m_hIcon, FALSE);        // Set small icon
  64.     
  65.    //
  66.     // Initialize BeeGrid
  67.    //
  68.  
  69.    try
  70.    {
  71.       m_grid.intf->DataMode    = sgBound;
  72.       m_grid.intf->AllowEdit   = VARIANT_TRUE;
  73.       m_grid.intf->AllowAddNew = VARIANT_TRUE;
  74.       m_grid.intf->AllowDelete = VARIANT_TRUE;
  75.  
  76.       // Change the connection string to match your configuration
  77.       LPCTSTR szProvider = _T("Provider=Microsoft.Jet.OLEDB.4.0;")
  78.                            _T("Data Source=F:\\Projects\\SampleData\\nwind.mdb;")
  79.                            _T("Persist Security Info=False");
  80.  
  81.       // Open connection object
  82.       ADODB::_ConnectionPtr spConnection(_T("ADODB.Connection"));
  83.       spConnection->CursorLocation = ADODB::adUseClient;
  84.       spConnection->Open(szProvider, _T(""), _T(""), ADODB::adOpenUnspecified);   
  85.  
  86.       // Pack connection pointer into the variant needed by recordset object
  87.       IDispatchPtr spConnectionDisp = spConnection;
  88.       _variant_t varConnection = spConnectionDisp.GetInterfacePtr();
  89.       
  90.       // Retrieve a recordset
  91.       ADODB::_RecordsetPtr spRecordset(_T("ADODB.Recordset"));
  92.       spRecordset->Open(_T("Products"), varConnection, ADODB::adOpenStatic,
  93.                         ADODB::adLockOptimistic, ADODB::adCmdTable);
  94.  
  95.       // Bind grid
  96.       MSDATASRC::DataSourcePtr spDataSource = spRecordset;
  97.       m_grid.intf->DataSource = spDataSource;
  98.    }
  99.    catch (_com_error& e)
  100.    {
  101.       CString err;
  102.       err.Format("Error# %08X\n%s\0", e.Error(), LPCSTR(e.Description()));
  103.       AfxMessageBox(err);
  104.    }
  105.    
  106.     return TRUE;  // return TRUE  unless you set the focus to a control
  107. }
  108.  
  109.  
  110. // If you add a minimize button to your dialog, you will need the code below
  111. //  to draw the icon.  For MFC applications using the document/view model,
  112. //  this is automatically done for you by the framework.
  113.  
  114. void CADODlg::OnPaint() 
  115. {
  116.     if (IsIconic())
  117.     {
  118.         CPaintDC dc(this); // device context for painting
  119.  
  120.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  121.  
  122.         // Center icon in client rectangle
  123.         int cxIcon = GetSystemMetrics(SM_CXICON);
  124.         int cyIcon = GetSystemMetrics(SM_CYICON);
  125.         CRect rect;
  126.         GetClientRect(&rect);
  127.         int x = (rect.Width() - cxIcon + 1) / 2;
  128.         int y = (rect.Height() - cyIcon + 1) / 2;
  129.  
  130.         // Draw the icon
  131.         dc.DrawIcon(x, y, m_hIcon);
  132.     }
  133.     else
  134.     {
  135.         CDialog::OnPaint();
  136.     }
  137. }
  138.  
  139. // The system calls this to obtain the cursor to display while the user drags
  140. //  the minimized window.
  141. HCURSOR CADODlg::OnQueryDragIcon()
  142. {
  143.     return (HCURSOR) m_hIcon;
  144. }
  145.  
  146. BEGIN_EVENTSINK_MAP(CADODlg, CDialog)
  147.     //{{AFX_EVENTSINK_MAP(CADODlg)
  148.     //}}AFX_EVENTSINK_MAP
  149. END_EVENTSINK_MAP()
  150.  
  151.  
  152.