我用ListView显示数据库中的内容,可是为什么它只显示数据库的第一个字段的内容?
解决方案 »
- 如何运行一段机器码?
- 游戏窗口弹出消息框, 有办法获取里面的文字信息没有?
- ADO访问sql server 2000(sp4)如何判断连接断开?
- 有谁了解曲线偏移的算法(autocad和visio中都有),给个方向好查资料
- 用VC 2005开发静态库遇到的两个问题
- 请问怎么判断CString中的字符串是中文还是英文的呢?
- createfont函数什么用处?怎么用?
- 请问各位前辈,请教一个关于使用LoadBitmap()的问题~~!
- 判断进程是否是管理员,测试windows核心编程代码,发现一个“bug"
- 求教如何在d3d程序内关闭和开启垂直同步.
- 高分征求解决方案
- 200分!!!谁能提供一份设计文档参考一下呀????我要写的标准一点的!!!!!
http://www.csdn.net/expert/topic/816/816910.xml?temp=.8320429
http://www.csdn.net/expert/topic/809/809890.xml?temp=.1718256
http://www.csdn.net/expert/topic/820/820683.xml?temp=.1690943谢了!!!
void CDatabaseView::OnGetdispinfo(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
// TODO: Add your control notification handler code here
TCHAR szValue[MAX_PATH];
szValue[0]='\0';
CDaoDatabase Database;
try
{
Database.Open("e:\\jh\\vc\\ShaoYe\\DataBase\\System_97.mdb");
}
catch(CDaoException* e)
{
MessageBox("无法打开数据库!","错误",MB_OK|MB_ICONERROR);
e->Delete();
}
CDaoRecordset RecordSet(&Database);
// CDaoRecordset *pRecordSet=new CDaoRecorset(&Database); try
{
RecordSet.Open(dbOpenDynaset,_T("SELECT * FROM Operation"));
}
catch(CDaoException* e)
{
MessageBox("无法打开数据表!","错误",MB_OK|MB_ICONERROR);
e->Delete();
} RecordSet.MoveLast(); COleVariant varValue;
long index=pDispInfo->item.iItem;
long subItem=pDispInfo->item.iSubItem;
CString str=pDispInfo->item.pszText;
str.GetLength(); if(pDispInfo->item.mask&LVIF_TEXT)
{
try
{
RecordSet.SetAbsolutePosition(index);
}
catch(CDaoException *e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription,MB_ICONEXCLAMATION);
e->Delete();
return;
} try
{
if(subItem)
RecordSet.GetFieldValue(subItem,varValue);
else
RecordSet.GetFieldValue(0,varValue);
}
catch(CDaoException *e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription,MB_ICONEXCLAMATION);
e->Delete();
return;
} const VARIANT *variant=LPCVARIANT(varValue); if(variant->vt&VT_BYREF)
return; switch(variant->vt)
{
case VT_ERROR:
{
strcpy(szValue,"Error");
break;
}
case VT_I2:
{
wsprintf(szValue,"%d",variant->iVal);
break;
}
case VT_I4:
{
wsprintf(szValue,"%d",variant->lVal);
break;
}
case VT_R4:
{
wsprintf(szValue,"%f",variant->fltVal);
break;
}
case VT_R8:
{
wsprintf(szValue,"%f",variant->dblVal);
break;
}
case VT_CY:
{
COleCurrency c(varValue);
CString s=c.Format();
strcpy(szValue,s.GetBuffer(s.GetLength()));
s.ReleaseBuffer();
break;
}
case VT_DATE:
{
COleDateTime t(variant->date);
CString s=t.Format("%B %d,%Y");
strcpy(szValue,s.GetBuffer(s.GetLength()));
s.ReleaseBuffer();
break;
}
case VT_BOOL:
{
if(variant->boolVal)
strcpy(szValue,"TRUE");
else
strcpy(szValue,"FALSE");
break;
}
case VT_UI1:
{
strcpy(szValue,(char *)variant->bVal);
break;
}
default:
{
szValue[0]='\0';
break;
}
}
lstrcpyn(pDispInfo->item.pszText,szValue,pDispInfo->item.cchTextMax);
} if(pDispInfo->item.mask&LVIF_IMAGE)
pDispInfo->item.iImage=0;// RecordSet.Close();
// Database.Close(); *pResult = 0;
}
{
LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
// TODO: Add your control notification handler code here
TCHAR szValue[MAX_PATH];
szValue[0]='\0';
CDaoDatabase Database;
try
{
Database.Open("e:\\jh\\vc\\ShaoYe\\DataBase\\System_97.mdb");
}
catch(CDaoException* e)
{
MessageBox("无法打开数据库!","错误",MB_OK|MB_ICONERROR);
e->Delete();
}
CDaoRecordset RecordSet(&Database);
try
{
RecordSet.Open(dbOpenDynaset,_T("SELECT * FROM Operation"));
}
catch(CDaoException* e)
{
MessageBox("无法打开数据表!","错误",MB_OK|MB_ICONERROR);
e->Delete();
} RecordSet.MoveLast(); COleVariant varValue;
long index=pDispInfo->item.iItem;
long subItem=pDispInfo->item.iSubItem;
CString str=pDispInfo->item.pszText;
str.GetLength(); if(pDispInfo->item.mask&LVIF_TEXT)
{
try
{
RecordSet.SetAbsolutePosition(index);
}
catch(CDaoException *e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription,MB_ICONEXCLAMATION);
e->Delete();
return;
} try
{
if(subItem)
RecordSet.GetFieldValue(subItem,varValue);
else
RecordSet.GetFieldValue(0,varValue);
}
catch(CDaoException *e)
{
AfxMessageBox(e->m_pErrorInfo->m_strDescription,MB_ICONEXCLAMATION);
e->Delete();
return;
} const VARIANT *variant=LPCVARIANT(varValue); if(variant->vt&VT_BYREF)
return; switch(variant->vt)
{
case VT_ERROR:
{
strcpy(szValue,"Error");
break;
}
case VT_I2:
{
wsprintf(szValue,"%d",variant->iVal);
break;
}
case VT_I4:
{
wsprintf(szValue,"%d",variant->lVal);
break;
}
case VT_R4:
{
wsprintf(szValue,"%f",variant->fltVal);
break;
}
case VT_R8:
{
wsprintf(szValue,"%f",variant->dblVal);
break;
}
case VT_CY:
{
COleCurrency c(varValue);
CString s=c.Format();
strcpy(szValue,s.GetBuffer(s.GetLength()));
s.ReleaseBuffer();
break;
}
case VT_DATE:
{
COleDateTime t(variant->date);
CString s=t.Format("%B %d,%Y");
strcpy(szValue,s.GetBuffer(s.GetLength()));
s.ReleaseBuffer();
break;
}
case VT_BOOL:
{
if(variant->boolVal)
strcpy(szValue,"TRUE");
else
strcpy(szValue,"FALSE");
break;
}
case VT_UI1:
{
strcpy(szValue,(char *)variant->bVal);
break;
}
default:
{
szValue[0]='\0';
break;
}
}
lstrcpyn(pDispInfo->item.pszText,szValue,pDispInfo->item.cchTextMax);
} if(pDispInfo->item.mask&LVIF_IMAGE)
pDispInfo->item.iImage=0; *pResult = 0;
}
CListCtrl *m_ListCtrl; //对应的列表控件
int CDBGridView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_ListCtrl=&GetListCtrl(); //得到列表视控件
////修改列表视风格
m_ListCtrl->ModifyStyle (0,LVS_REPORT);
m_ListCtrl->SetExtendedStyle (LVS_EX_GRIDLINES|
LVS_EX_FULLROWSELECT);
BuildColumns(); //建立要显示的列
DisplayDB(); //显示数据 return 0;
}void CDBGridView::BuildColumns()
{
int nCols=7; //列数
int nColSize[]={50,100,100,100,80,80,700}; //每列宽度
static char *nColLabel[]=
{
"ID",
"编号",
"名称",
"内容",
"日期",
"金额",
"备注"
};
LV_COLUMN lvCol;
lvCol.mask=LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
for (int i=0;i<nCols;i++)
{
lvCol.iSubItem=i;
lvCol.pszText=nColLabel[i];
lvCol.cx=nColSize[i];
lvCol.fmt=LVCFMT_LEFT;
m_ListCtrl->InsertColumn(i,&lvCol);
}
}数据库部分上面好像有了
ID 编号 名称 日期 金额
9
10
11
12
13
这些我已经显示在第一排,
关键问题是显示只显示ID这个字段的内容,其他字段的内容则没有?
这是什么原因?
你可以提供具体一点的知道嘛?
哥哥,谢谢了!
用RecordSet.GetFieldCount()得到字段的列数,可是怎么写这个具体的程序?在哪里写这个循环?
int nFields;
CDaoTableDef td(m_pDatabase);
try
{
td.Open(m_strTableName);
nFields=td.GetFieldCount();
for(int j=0;j<nFields;j++){
td.GetFieldInfo(j,fieldInfo);
int nWidth=m_Listview.GetStringWidth(fieldInfo.m_strName)+30;
m_Listview.InsertColumn(j,fieldInfo.m_strName,LVCFMT_LEFT,nWidth);
}
}
catch(CDaoException* e)
{
e->ReportError();
e->Delete();
return;
}
td.Close();
//读取表数据
int nItem=0;
m_pImageList = new CImageList();
m_pImageList->Create(IDB_IMAGELIST, 16, 1, RGB(0,0,0));
try
{
CString strSelect=(_T("Select * From["));
strSelect+=m_strTableName;
strSelect+=_T("]");
m_pRecordset->Open(dbOpenDynaset,strSelect);
while(!m_pRecordset->IsEOF()){
COleVariant var;
var=m_pRecordset->GetFieldValue(0);
m_Listview.InsertItem(nItem,CCrack::strVARIANT(var));
for(int i=0;i<nFields;i++){
m_Listview.SetImageList(m_pImageList, LVSIL_SMALL);
var=m_pRecordset->GetFieldValue(i);
m_Listview.SetItemText(nItem,i,CCrack::strVARIANT(var));
}
nItem++;
m_pRecordset->MoveNext();
}
我的PreCreateWindow是这样:
BOOL CJwglView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= LVS_REPORT;
return CListView::PreCreateWindow(cs);
}
{
m_ListCtr.DeleteAllItems();
while (m_ListCtr.DeleteColumn(0));
try
{
HRESULT hr=m_pSet->Open((_bstr_t)m_sql,\
&(*m_pCon),adOpenForwardOnly,adLockOptimistic,adCmdText);
// VARIANT var;
if(SUCCEEDED(hr))
{
long i; //i用来记录返回列的数目
m_pSet->Fields->get_Count(&i);
//m_pSet->Fields->GetItem(0)
CString str;
for(int n=0;n<i;n++)
{
m_Fid=m_pSet->Fields->GetItem((short)n);//n是字段的索引号
DataTypeEnum m_type;
m_type=m_Fid->GetType();//返回字段的类型
if(m_type==adDouble||m_type==adInteger)
m_ListCtr.InsertColumn(n,m_Fid->GetName(),LVCFMT_CENTER,60);//往ListCtrl中加入标题列名
else
m_ListCtr.InsertColumn(n,m_Fid->GetName(),LVCFMT_CENTER,110);
}
m_pSet->MoveFirst();
int m=0;
CString temp;
while(m_pSet->EndOfFile==VARIANT_FALSE)
{
for(int j=0;j<i;j++)
{
m_Fid=m_pSet->Fields->GetItem((short)j);
//m_pSet->GetCollect();
/////////valuetostring();
if(m_Fid->Value.vt!=VT_NULL)
{
temp=ValuetoString(m_Fid->Value);
if(j==0)
m_ListCtr.InsertItem(m,temp);
else
m_ListCtr.SetItemText(m,j,temp);
}
}
m_pSet->MoveNext();
m++;
}///end while
m_pSet->Close();
}//end Hr
}///end try
catch(_com_error *e)
{
CString Error = e->ErrorMessage();
AfxMessageBox(Error);
return;
}
}