(百度和网页http://hi.baidu.com/guxue365/blog/item/34af638b08d1c119c9fc7aef.html的作者无关,不对其内容负责。百度快照谨为网络故障时之索引,不代表被搜索网站的即时页面。)
CDataGrid、CAdodc与C_RecordSet的使用细节
2010-01-24 09:24
1.查询指定USERID的数据并在与ADO捆绑好的数据表格控件CDataGrid中显示
(CDataGrid与CAdodc、C_Recordset搭配使用,进行定置查询、显示数据):
CString strRecordSource = m_oADO.GetRecordSource();
CString strConnect = m_oADO.GetConnectionString();
C_Recordset oRecord = m_oADO.GetRecordset();
long lCount = oRecord.GetRecordCount();
//VARIANT oSource = oRecord.GetSource();
long lCursorType = oRecord.GetCursorType();
long lLockType = oRecord.GetLockType();
COleVariant oleVar("select * from LEDOPERATLOG where USERID = 8");
oRecord.Close();
oRecord.Open( oleVar,COleVariant(strConnect),lCursorType, lLockType, adConnectUnspecified );
oRecord.Requery( adConnectUnspecified );
m_ctrlDBGrid.Refresh();
CString strConnect = m_oADO.GetConnectionString();
C_Recordset oRecord = m_oADO.GetRecordset();
long lCount = oRecord.GetRecordCount();
//VARIANT oSource = oRecord.GetSource();
long lCursorType = oRecord.GetCursorType();
long lLockType = oRecord.GetLockType();
COleVariant oleVar("select * from LEDOPERATLOG where USERID = 8");
oRecord.Close();
oRecord.Open( oleVar,COleVariant(strConnect),lCursorType, lLockType, adConnectUnspecified );
oRecord.Requery( adConnectUnspecified );
m_ctrlDBGrid.Refresh();
2.得表格中鼠标当前所在行号:
VARIANT oVarB = m_ctrlGrid.GetBookmark();
3.得到、移除、增加表格中选中的行(表格在按下Ctrl键时可选中多行):
CSelBookmarks oBookMarks = m_ctrlGrid.
GetSelBookmarks();
long lSel = oBookMarks.GetCount();
VARIANT oVarBook = oBookMarks.GetItem( COleVariant(0L) );
long lSel = oBookMarks.GetCount();
VARIANT oVarBook = oBookMarks.GetItem( COleVariant(0L) );
oBookMarks.Add(...);
oBookMarks.Remove(..);
4.删除CDataGrid中选中的多行记录:
*此时有一个问题是要特别注意的:多选的记录可能并不在表格的当前显示界面上(滚动后再选中了要删除
的记录 )
CSelBookmarks oBookMarks = m_ctrlGrid.
GetSelBookmarks();
if( oBookMarks.GetCount() <= 0 )
{
AfxMessageBox( "当前没有选中的记录!用鼠标点击要删除记录的头部,使整行记录被选中! \r\n \r\n\r\n*按下Ctrl鍵可选中多个记录 " );
return;
}
if( oBookMarks.GetCount() <= 0 )
{
AfxMessageBox( "当前没有选中的记录!用鼠标点击要删除记录的头部,使整行记录被选中! \r\n \r\n\r\n*按下Ctrl鍵可选中多个记录 " );
return;
}
int nRowCurrent = m_ctrlGrid.GetRow();
for( long i = 0; i < oBookMarks.GetCount() ; i++ )
{
ST_DEVICE_INFO oDeviceInfo;
for( long i = 0; i < oBookMarks.GetCount() ; i++ )
{
ST_DEVICE_INFO oDeviceInfo;
VARIANT oVar = oBookMarks.GetItem( COleVariant(i) );
long lRowMove = oVar.dblVal - 1;
m_oADO.GetRecordset().MoveFirst();
m_oADO.GetRecordset().Move( lRowMove,COleVariant(0L) );
m_oADO.GetRecordset().MoveFirst();
m_oADO.GetRecordset().Move( lRowMove,COleVariant(0L) );
//3 address; 6 id; 10 IP;
CField oField_Address = m_oADO.GetRecordset().GetFields().GetItem( COleVariant(3L) );
VARIANT oVarAddress = oField_Address.GetValue();
CField oField_ID = m_oADO.GetRecordset().GetFields().GetItem( COleVariant(6L) );
VARIANT oVarID = oField_ID.GetValue();
VARIANT oVarID = oField_ID.GetValue();
CField oField_IP = m_oADO.GetRecordset().GetFields().GetItem( COleVariant(10L) );
VARIANT oVarIP = oField_IP.GetValue();
VARIANT oVarIP = oField_IP.GetValue();
oDeviceInfo.m_dwDeviceAddress = oVarAddress.iVal;
sprintf( oDeviceInfo.m_cDeviceID, (CString)oVarID.bstrVal );
sprintf( oDeviceInfo.m_cIP, (CString)oVarIP.bstrVal );
vctDeviceInfo.push_back( oDeviceInfo );
}
sprintf( oDeviceInfo.m_cDeviceID, (CString)oVarID.bstrVal );
sprintf( oDeviceInfo.m_cIP, (CString)oVarIP.bstrVal );
vctDeviceInfo.push_back( oDeviceInfo );
}
for( int iIndex = 0; iIndex < vctDeviceInfo.size(); iIndex++ )
BOOL bDelRow = oTableDeviceInfo.DelRow( vctDeviceInfo[iIndex] );
BOOL bDelRow = oTableDeviceInfo.DelRow( vctDeviceInfo[iIndex] );
GridShowAllRecords(); //重新执行SQL语句,关联到表格后再让表格的显示;具体如上1.中所述
5.改变绑定的数据源:
通过UDL改变CAdodc在IDE中指定的数据源的方法如下:
//设备数据源 .ConnectionString ="File
CString strConn = m_oADO.GetConnectionString();
CString strCurPath; char cPath[256];
::GetCurrentDirectory( 255, cPath );
strCurPath = (CString)cPath + "\\" + CONST_FILE_NAME_UDL;
m_oADO.SetConnectionString( "File + strCurPath );
strConn = m_oADO.GetConnectionString();
m_oADO.Refresh(); //数据源刷新
CString strConn = m_oADO.GetConnectionString();
CString strCurPath; char cPath[256];
::GetCurrentDirectory( 255, cPath );
strCurPath = (CString)cPath + "\\" + CONST_FILE_NAME_UDL;
m_oADO.SetConnectionString( "File + strCurPath );
strConn = m_oADO.GetConnectionString();
m_oADO.Refresh(); //数据源刷新
m_ctrlGrid.Refresh(); //表格刷新
*通常以上代码放在On
InitDialog()中;
6.光标的使用:??光标有何作用
long lCursorCur3 = m_oADO.GetRecordset().GetCursorLocation();
7.手工创建的连接方式(网摘,未试验过):
下面是我以前用过的一段程序,程序来动态创建adodc和
datagrid空件,和动态绑定adodc,
datagrid:
-------------------------
CAdodc * m_padodc;
CDataGrid * m_pdatagrid;
CRect rect(1,1,10,10);
CRect grid(1,20,100,200);
m_padodc=new CAdodc;
m_padodc->Create(NULL,WS_CHILD,rect,this,7999);
m_padodc->SetConnectionString(...);
m_padodc->SetCommandType(1);
m_padodc->SetRecordSource(...);
m_pdatagrid=new CDataGrid;
m_pdatagrid->Create(NULL,_T(""),WS_CHILD, grid, this,7998);
//动态绑定
LPUNKNOWN pCursor=m_padodc->GetDSCCursor();
ASSERT(pCursor!=NULL);
m_pdatagrid->BindProperty(0x9,m_padodc);
m_pdatagrid->SetRefDataSource(pCursor);
--------------------------------------
对不起,是这样的,将两个CREATE语句中的WS_CHILD换成WS_CHILD|WS_VISIBLE即可。
-------------------------
CAdodc * m_padodc;
CDataGrid * m_pdatagrid;
CRect rect(1,1,10,10);
CRect grid(1,20,100,200);
m_padodc=new CAdodc;
m_padodc->Create(NULL,WS_CHILD,rect,this,7999);
m_padodc->SetConnectionString(...);
m_padodc->SetCommandType(1);
m_padodc->SetRecordSource(...);
m_pdatagrid=new CDataGrid;
m_pdatagrid->Create(NULL,_T(""),WS_CHILD, grid, this,7998);
//动态绑定
LPUNKNOWN pCursor=m_padodc->GetDSCCursor();
ASSERT(pCursor!=NULL);
m_pdatagrid->BindProperty(0x9,m_padodc);
m_pdatagrid->SetRefDataSource(pCursor);
--------------------------------------
对不起,是这样的,将两个CREATE语句中的WS_CHILD换成WS_CHILD|WS_VISIBLE即可。