

如上第一第二張圖所示,使用的是定時器持續讀取txt檔案的資料到ListControl控制元件中,但是清空串列框資料之后再重新讀取txt檔案再寫入List Control控制元件后,使用了DeleteAllItems后再InsertItem就沒有資料顯示 只有標號 希望有大神解答我的疑惑 謝謝!
定時器代碼主要就是呼叫兩個函式: showList(); writeInFile();
void CBoschItemDlg::OnBnClickedButton2() //觸發第二個定時器 讀取txt檔案資料并寫入資料庫
{
GetDlgItemText(IDC_EDIT8, Operator);
if (Operator.IsEmpty())
{
AfxMessageBox("請輸入操作人!");
}
else
{
m_List.DeleteAllItems();
SetTimer(2, 1000, NULL);
}
}
void CBoschItemDlg::showList()
{
strCount.Format("%d", countRecord);
strCountID.Format("%d", countR);
m_List.InsertItem(countRecord, strCountID);
m_List.SetItemText(countRecord, 1, ProductID);
m_List.SetItemText(countRecord, 2, strRangeT);
m_List.SetItemText(countRecord, 3, strRangeA);
m_List.SetItemText(countRecord, 4, strnumTorque);
m_List.SetItemText(countRecord, 5, strnumAngle);
m_List.SetItemText(countRecord, 6, Result);
m_List.SetItemText(countRecord, 7, Operator);
m_List.SetItemText(countRecord, 8, m_strTime);
countRecord++;
countR++;
}
void CBoschItemDlg::writeInFile()
{
try
{
m_pRecordset->AddNew();
m_pRecordset->PutCollect("ProductID", _variant_t(ProductID));
m_pRecordset->PutCollect("TorqueRange", _variant_t(strRangeT));
m_pRecordset->PutCollect("AngleRange", _variant_t(strRangeA));
m_pRecordset->PutCollect("TorqueValue", _variant_t(strnumTorque));
m_pRecordset->PutCollect("AngleValue", _variant_t(strnumAngle));
m_pRecordset->PutCollect("Result", _variant_t(Result));
m_pRecordset->PutCollect("Operator", _variant_t(Operator));
m_pRecordset->PutCollect("RecordTime", _variant_t(m_strTime));
m_pRecordset->Update();
}
catch (_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
uj5u.com熱心網友回復:
網上摘抄,解決方法如下(建議嘗試一下):
m_tree.SetRedraw(FALSE);
//do erase and insert operation
m_tree.SetRedraw(TRUE);
m_tree.RedrawWindow();
都是網上搜索到的解決方案,百度排名前幾,,,
http://www.cppblog.com/gracelee/archive/2006/06/16/8623.html
uj5u.com熱心網友回復:
不行呀 試過了 還是這樣啊
uj5u.com熱心網友回復:
單步除錯,確實你那些字串是否為Emptyuj5u.com熱心網友回復:
嗯 字串是不為空的 只是在顯示的時候 執行了一遍DeleteAllItems()后,再InsertItem它就是沒有資料 但是寫入資料庫是有值的 所以我覺得是deleteAllItems的問題 但是確實不知道怎么改
uj5u.com熱心網友回復:
CListCtrl::UpdateBOOL Update( int nItem );
Return Value
Nonzero if successful; otherwise zero.
Parameters
nItem
Index of the item to be updated.
Remarks
Call this function to force the list view control to repaint the item specified by nItem. This function also arranges the list view control if it has the LVS_AUTOARRANGE style.
CListCtrl Overview | Class Members | Hierarchy Chart
See Also CListCtrl::DrawItem
uj5u.com熱心網友回復:
void CBoschItemDlg::showList(){
strCount.Format("%d", countRecord);
strCountID.Format("%d", countR);
m_List.InsertItem(countRecord, strCountID);
m_List.SetItemText(countRecord, 1, ProductID);
m_List.SetItemText(countRecord, 2, strRangeT);
m_List.SetItemText(countRecord, 3, strRangeA);
m_List.SetItemText(countRecord, 4, strnumTorque);
m_List.SetItemText(countRecord, 5, strnumAngle);
m_List.SetItemText(countRecord, 6, Result);
m_List.SetItemText(countRecord, 7, Operator);
m_List.SetItemText(countRecord, 8, m_strTime);
countRecord++; //問題就在這里啊. 你清空表格, 但這個值又沒有變它
countR++;
}
正確的做法是這樣.
int nCount = m_List.GetItemCount(); //插入到末尾
if(nCount>0)
{
nCount -=1;
}
int index = m_List.InsertItem(nCount, strCountID); //得到直正的插入行位置
m_List.SetItemText(index , 1, ProductID); //設定其它列
m_List.SetItemText(index , 2, strRangeT);
m_List.SetItemText(index , 3, strRangeA);
m_List.SetItemText(index , 4, strnumTorque);
m_List.SetItemText(index , 5, strnumAngle);
m_List.SetItemText(index , 6, Result);
m_List.SetItemText(index , 7, Operator);
m_List.SetItemText(index , 8, m_strTime);
uj5u.com熱心網友回復:
一般不出資料可能是以下幾種情況,1.資料本身為空,
2.下標沒有從0開始插入。
3.資料加載錯了控制元件。。
uj5u.com熱心網友回復:
問題解決!可以顯示資料了!謝謝你!
uj5u.com熱心網友回復:
問題已解決!謝謝大家的熱心回答和幫助!謝謝!!!uj5u.com熱心網友回復:
insertitem之前,最好先獲取下count,否則行序號容易搞錯,當然如果業務邏輯清楚就沒有必要了,通過變數控制也可以的。轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/93915.html
標籤:界面
