请教如何将数据库中的数据显示在datagrid控件上(具体用哪些函数),谢谢!

时间:2022-09-21 19:12:10
求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!求助!

7 个解决方案

#1


需要用 ADO Data Control 来配置数据源,然后在Data Grid里的Data Source 里选择ADO Data Control的ID

#2


http://www.vckbase.com/document/viewdoc/?id=261


http://www.vckbase.com/document/viewdoc/?id=598

#3


CDataGrid       m_wndGrid;
m_wndGrid.SetRefDataSource(NULL);
m_wndGrid.SetRefDataSource((LPUNKNOWN)m_adoRecordSet->GetRecordset());
m_wndGrid.SetAllowAddNew(TRUE);
m_wndGrid.SetAllowDelete(TRUE);
m_wndGrid.SetAllowUpdate(TRUE);
m_wndGrid.Refresh();

#4


上面错了 
m_wndGrid.SetRefDataSource((LPUNKNOWN)m_adoRecordSet->GetRecordset());
这句就是设置数据源,应该是    _RecordsetPtr  m_pRecordset;//你自己定义的记录对象
             m_wndGrid.SetRefDataSource(m_pRecordset);

#5


马上试试.

#6


我用的是m_wndGrid.SetRefDataSource(m_pRecordset);但还是抛出错误!
用ADO Control倒是可以把数据显示在DataGrid中,但是Record Source中Command Text(SQL)中好像只能写一条语句,怎么可以自己设定啊?

#7


BOOL CDlgRecordset::QueryRecord()
{
_CommandPtr cmd;  
_RecordsetPtr rs;  
_ConnectionPtr conn; 
_variant_t vra;
VARIANT *vt1 = NULL;
try
{
/* cmd.CreateInstance( __uuidof(Command));
rs.CreateInstance(__uuidof(Recordset));
conn.CreateInstance(__uuidof(Connection));*/

UpdateData(TRUE);
theApp.m_pConnection->PutCursorLocation(adUseClient);

/* conn->CursorLocation = adUseClient;

conn->Open(_bstr_t( theApp.m_pConnection.GetInterfacePtr() ), L"", L"", -1);
cmd->ActiveConnection = conn;

   cmd->CommandText = (_bstr_t) m_strQuery;
cmd->CommandType = adCmdText;
rs = cmd->Execute(&vra,vt1, adCmdText);*/
/*rs->PutRefActiveConnection(theApp.m_pConnection);
rs->Open((_bstr_t)m_strQuery,
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);*/
///     ((CTestDllApp*)(&theApp))->m_pConnection
/// m_pRecordset.CreateInstance(__uuidof(Recordset));

/* m_pRecordset->Open((_bstr_t)m_strQuery,
_variant_t((IDispatch*)theApp.m_pConnection, TRUE),
adOpenStatic,
adLockOptimistic,
adCmdText);*/
   if(m_pRecordset->State==1)
{
m_pRecordset->Close();
}
m_pRecordset->Open((_bstr_t)m_strQuery,
theApp.m_pConnection.GetInterfacePtr(),//»ñÈ¡¿â½Ó¿âµÄIDispatchÖ¸Õë
adOpenStatic,
adLockOptimistic,
adCmdText);
/* m_DataGrid.SetRefDataSource(NULL);
m_DataGrid.SetRefDataSource((LPUNKNOWN)m_pRecordset);
m_DataGrid.Refresh();*/
}
catch (_com_error &e)
{
::MessageBox(NULL,e.Description(),"Ìáʾ",MB_OK);
}

m_DataGrid.SetRefDataSource((LPUNKNOWN)m_pRecordset);
m_DataGrid.Refresh();
UpdateData(FALSE);
//m_pRecordset->Close();
return true;
 
}

#1


需要用 ADO Data Control 来配置数据源,然后在Data Grid里的Data Source 里选择ADO Data Control的ID

#2


http://www.vckbase.com/document/viewdoc/?id=261


http://www.vckbase.com/document/viewdoc/?id=598

#3


CDataGrid       m_wndGrid;
m_wndGrid.SetRefDataSource(NULL);
m_wndGrid.SetRefDataSource((LPUNKNOWN)m_adoRecordSet->GetRecordset());
m_wndGrid.SetAllowAddNew(TRUE);
m_wndGrid.SetAllowDelete(TRUE);
m_wndGrid.SetAllowUpdate(TRUE);
m_wndGrid.Refresh();

#4


上面错了 
m_wndGrid.SetRefDataSource((LPUNKNOWN)m_adoRecordSet->GetRecordset());
这句就是设置数据源,应该是    _RecordsetPtr  m_pRecordset;//你自己定义的记录对象
             m_wndGrid.SetRefDataSource(m_pRecordset);

#5


马上试试.

#6


我用的是m_wndGrid.SetRefDataSource(m_pRecordset);但还是抛出错误!
用ADO Control倒是可以把数据显示在DataGrid中,但是Record Source中Command Text(SQL)中好像只能写一条语句,怎么可以自己设定啊?

#7


BOOL CDlgRecordset::QueryRecord()
{
_CommandPtr cmd;  
_RecordsetPtr rs;  
_ConnectionPtr conn; 
_variant_t vra;
VARIANT *vt1 = NULL;
try
{
/* cmd.CreateInstance( __uuidof(Command));
rs.CreateInstance(__uuidof(Recordset));
conn.CreateInstance(__uuidof(Connection));*/

UpdateData(TRUE);
theApp.m_pConnection->PutCursorLocation(adUseClient);

/* conn->CursorLocation = adUseClient;

conn->Open(_bstr_t( theApp.m_pConnection.GetInterfacePtr() ), L"", L"", -1);
cmd->ActiveConnection = conn;

   cmd->CommandText = (_bstr_t) m_strQuery;
cmd->CommandType = adCmdText;
rs = cmd->Execute(&vra,vt1, adCmdText);*/
/*rs->PutRefActiveConnection(theApp.m_pConnection);
rs->Open((_bstr_t)m_strQuery,
theApp.m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);*/
///     ((CTestDllApp*)(&theApp))->m_pConnection
/// m_pRecordset.CreateInstance(__uuidof(Recordset));

/* m_pRecordset->Open((_bstr_t)m_strQuery,
_variant_t((IDispatch*)theApp.m_pConnection, TRUE),
adOpenStatic,
adLockOptimistic,
adCmdText);*/
   if(m_pRecordset->State==1)
{
m_pRecordset->Close();
}
m_pRecordset->Open((_bstr_t)m_strQuery,
theApp.m_pConnection.GetInterfacePtr(),//»ñÈ¡¿â½Ó¿âµÄIDispatchÖ¸Õë
adOpenStatic,
adLockOptimistic,
adCmdText);
/* m_DataGrid.SetRefDataSource(NULL);
m_DataGrid.SetRefDataSource((LPUNKNOWN)m_pRecordset);
m_DataGrid.Refresh();*/
}
catch (_com_error &e)
{
::MessageBox(NULL,e.Description(),"Ìáʾ",MB_OK);
}

m_DataGrid.SetRefDataSource((LPUNKNOWN)m_pRecordset);
m_DataGrid.Refresh();
UpdateData(FALSE);
//m_pRecordset->Close();
return true;
 
}