有如下代码,可是有最后一个记录可以显示,前面的全是空的,如果把m_pList->SetItemText()中的index设为0,则可以正常显示。难道insertItem()是插在前面的? 相同的代码在另一个程序中能正常?这是为什么?还是在m_pList->Create()时可以指定插入头还是尾? 其中m_OFDataSet是由CRecordset类派生的一个类的实例。
while(!m_OFDataSet->IsEOF())
  {                       
                       m_pList->InsertItem(index,"");
  
  m_pList->SetItemText(index,0,m_OFDataSet->m_a1);
  
  m_pList->SetItemText(index,1,m_OFDataSet->m_a2);
  
  m_pList->SetItemText(index,2,m_OFDataSet->m_a3);
  
  m_pList->SetItemText(index,3,m_OFDataSet->m_a4);
  
  m_pList->SetItemText(index,4,m_OFDataSet->m_a5);
  
  m_pList->SetItemText(index,5,m_OFDataSet->m_a6);
  
  m_pList->SetItemText(index,6,m_OFDataSet->m_a7);
  
  m_pList->SetItemText(index,7,m_OFDataSet->m_a8);
  
//   m_pList->SetItemText(index,1,m_OFDataSet->m_a1);
      
  index++;
  m_OFDataSet->MoveNext();
  
  }

解决方案 »

  1.   

    我一般这样用LONG index = m_pList->InsertItem(-1,"");m_pList->SetItemText(index,0,m_OFDataSet->m_a1);(以下签名由MyCSDN回复工具生成)
    -----------------------------------------------
    MyCSDN 免费版 - http://community.csdn.net/Expert/TopicView1.asp?id=4608614
      

  2.   

    但是我如果把InsertItem()中的index换成0(数字零),那么显示完全正常,还有,如果先用whlie(!m_OFDataSet->IsEOF()){
           InsertItem(index,...);
           index++
           }
    把Item添好,然后再用另一个while循环调用SetItemText(index,);就可以。
      

  3.   

    你的写法错误,0对了是巧合。MSDN上:MFC Library Reference CListCtrl::InsertItemSee Also
    CListCtrl Overview | Class Members | Hierarchy Chart | CListCtrl::DeleteItem | CListCtrl::DeleteAllItemsInserts an item into the list view control.
    int InsertItem(
       const LVITEM* pItem 
    );
    int InsertItem(
       int nItem,
       LPCTSTR lpszItem 
    );
    int InsertItem(
       int nItem,
       LPCTSTR lpszItem,
       int nImage 
    );
    Parameters
    pItem 
    Pointer to an LVITEM structure that specifies the item's attributes, as described in the Platform SDK. 
    nItem 
    Index of the item to be inserted. 
    lpszItem 
    Address of a string containing the item's label, or LPSTR_TEXTCALLBACK if the item is a callback item. For information on callback items, see CListCtrl::GetCallbackMask. 
    nImage 
    Index of the item's image, or I_IMAGECALLBACK if the item is a callback item. For information on callback items, see CListCtrl::GetCallbackMask. 
    Return Value
    The index of the new item if successful or -1 otherwise.
    Example
    // The pointer to my list view control.
    extern CListCtrl* pmyListCtrl;CString strText;
    int nColumnCount = pmyListCtrl->GetHeaderCtrl()->GetItemCount();// Insert 10 items in the list view control.
    for (int i=0;i < 10;i++)
    {
       strText.Format(TEXT("item %d"), i);   // Insert the item, select every other item.
       pmyListCtrl->InsertItem(
          LVIF_TEXT|LVIF_STATE, i, strText, 
          (i%2)==0 ? LVIS_SELECTED : 0, LVIS_SELECTED,
          0, 0);   // Initialize the text of the subitems.
       for (int j=1;j < nColumnCount;j++)
       {
          strText.Format(TEXT("sub-item %d %d"), i, j);
          pmyListCtrl->SetItemText(i, j, strText);
       }
    }
    (以下签名由MyCSDN回复工具生成)
    -----------------------------------------------
    MyCSDN 免费版 - http://community.csdn.net/Expert/TopicView1.asp?id=4608614
      

  4.   

    关于返回值的说明:Return Value
    The index of the new item if successful or -1 otherwise.(以下签名由MyCSDN回复工具生成)
    -----------------------------------------------
    MyCSDN 免费版 - http://community.csdn.net/Expert/TopicView1.asp?id=4608614
      

  5.   

    找到原因了,因为我在调用create()的时候设置了样式,是用的升序,每插入一个空的项(值是空字符串),就排一次序,新项就被排到前面去了,结果SetItemText(index)设置最后一个项,去掉排序就一切正常了