我用ListView显示数据库中的内容,可是为什么它只显示数据库的第一个字段的内容?

解决方案 »

  1.   

    你这个问题太笼统了,鬼知道你代码怎么写的。请VC高手顺便解决一下:
    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谢了!!!
      

  2.   

    lightrain(大雨滂沱)
    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;
    }
      

  3.   

    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);
       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;
    }
      

  4.   

    先设置显示风格,和需要的列数
    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);

    }数据库部分上面好像有了
      

  5.   

    我不是用的DBGrid呀,
    ID 编号           名称         日期           金额
    9
    10
    11
    12
    13
    这些我已经显示在第一排,
    关键问题是显示只显示ID这个字段的内容,其他字段的内容则没有?
    这是什么原因?
      

  6.   

    other column. use SetItemText, you need let listview display it yourself.
      

  7.   

    zdhe(zdhe) 
    你可以提供具体一点的知道嘛?
    哥哥,谢谢了!
      

  8.   

    你只得到了了数据库的第一个字段,当然只能显示第一行 ID了,在打开数据库后,你要得到数据库字段的数量,然后用一个循环把所有的字段的数据显示出来,具体代码我记不清了,应该有一个函数的到记录集的字段数数量getfieldcount,得到所有记录的数量get??count,请参考msdn
      

  9.   

    winhero:
     用RecordSet.GetFieldCount()得到字段的列数,可是怎么写这个具体的程序?在哪里写这个循环?
      

  10.   

    CDaoFieldInfo fieldInfo;
    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);
    }
      

  11.   

    以下是我在CListCtrl里显示的一个例子,跟ListView是差不多的!void CWg1View::ShowList(CString m_sql)
    {
    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;
    }
         
    }
      

  12.   

    这个帖子6月份就发了,怎么现在还没有结贴!sigh