描述:
我的问题是这样:
我将一个datagrid控件置在对话框上,并将datagrid绑定到数据库的一张表。当建立对话框应用程序时,一切测试正常。
当我将整个对话框(包括其上的datagrid控件)封装成一个ocx时,就遇到无法显示的问题。
具体源代码如下(VC6.0环境下,项目名为dialogocx):
#include "dlg.h"
class CDialogOCXCtrl : public COleControl
{
DECLARE_DYNCREATE(CDialogOCXCtrl)
// Constructor
public:
CDialogOCXCtrl();
CDlg m_dialog;
int CDialogOCXCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_dialog.Create(IDD_DIALOG1,this);
m_dialog.ShowWindow(SW_SHOW);
return 0;
}
void CDialogOCXCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
// TODO: Replace the following code with your own drawing code.
// pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
//pdc->Ellipse(rcBounds);
m_dialog.MoveWindow(rcBounds);
}
BOOL CDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
_ConnectionPtr m_pConnection;
//m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection.CreateInstance("ADODB.Connection");
m_pConnection->Open("Provider=SQLOLEDB;server=(local);uid=sa;pwd=ad123456;database=pubs",
"","",adModeUnknown);
_RecordsetPtr pRS;
// pRS->SetDatabase(m_pConnection);
// pRS->Open(_T(select * from authors));
CString strSql;
strSql="select * from authors";
//pRS.CreateInstance(__uuidof(Recordset));
pRS.CreateInstance( "ADODB.Recordset");
pRS->Open((LPCTSTR)strSql, // 查询表中所有字段
//m_pConnection.GetInterfacePtr(),
_variant_t((IDispatch*)m_pConnection,true), // 获取库接库的IDispatch指针
adOpenStatic,
adLockOptimistic,
adCmdText);
//pRS = m_pConnection->GetRecordSet((_bstr_t)strSql);
m_ctrlDatagrid.SetRefDataSource(NULL);
m_ctrlDatagrid.SetCaption(_T("测试"));
m_ctrlDatagrid.SetRefDataSource((LPUNKNOWN)pRS);
m_ctrlDatagrid.Refresh();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}