在VC++中做控件与数据表字段绑定

 做管理系统难免与数据库打交道……我们都要将控件与数据表中的字读绑定……在VC++ 6.0中,提供了MFC ClassWizard,这很方便的就能实现这一功能……但是在VC++ 2008中,却去掉了ClassWizard这个功能……所以,这里就不那么方便了……

然而了解原理的都知道,这不过就是添加几个代码就搞定了事……

DDX_FieldText函数就是解决之道……

void AFXAPI DDX_FieldText(CDataExchange* pDX,   int nIDC,   COleCurrency& value,   CDaoRecordset* pRecordset);

pDX

A pointer to a <?XML:NAMESPACE PREFIX = MSHelp NS = "http://msdn.microsoft.com/mshelp" />CDataExchange object. The framework supplies this object to establish the context of the data exchange, including its direction.

nIDC

The ID of a control in the CRecordView or CDaoRecordView object.

value

A reference to a field data member in the associated CRecordset or CDaoRecordset object. The data type of value depends on which of the overloaded versions of DDX_FieldText you use.

pRecordset

A pointer to the CRecordset or CDaoRecordset object with which data is exchanged. This pointer enables DDX_FieldText to detect and set Null values.

例:

void CMyDaoRecordView::DoDataExchange(CDataExchange* pDX)
{
   CDaoRecordView::DoDataExchange(pDX);
   DDX_FieldCBString(pDX, IDC_LASTNAME, m_pSet->m_LastName, m_pSet);
   DDX_FieldText(pDX, IDC_ID, m_pSet->m_EmployeeID, m_pSet);
   DDX_FieldText(pDX, IDC_AGE, m_pSet->m_Age, m_pSet);
}

发布了17 篇原创文章 · 获赞 10 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/markinstephen/article/details/3062656