我设计了一个单文档的对话框 基于listview的窗体,在窗体里有一个listview控件(显示ACCESS数据库里的数据记录)但是当串口数据保存到数据库里后,数据库已经完成了添加记录后,在listview控件中并没用及时显示更新,只有在关闭窗口后再次运行应用程序时才能看到更新的记录。

解决方案 »

  1.   

    void CExam9_1View::Display()
    {
    CListCtrl& m_ListCtrl = GetListCtrl();

    Exam9_1Set sSet;
    int nItem = 0;
    CString str;
    sSet.m_strSort="time";
    sSet.Open();
          
         while (!sSet.IsEOF())
    { m_ListCtrl.InsertItem( nItem, sSet.m_address);
    //str=sSet.m_days.Format("%d-%m-%y"); 
    m_ListCtrl.SetItemText( nItem, 1,sSet.m_days);
    m_ListCtrl.SetItemText( nItem, 2,sSet.m_time);
    /*  int hour,mi,se;
    hour=sSet.m_days.GetHour();
    mi=sSet.m_days.GetMinute();
    se=sSet.m_days.GetSecond();
    str.Format("%02i:%02i:%02i", hour,mi,se);*/
    // str.Format("%4.1f", sSet.m_RH);

    str.Format("%3.1f", sSet.m_Votage);
    m_ListCtrl.SetItemText( nItem, 3, str);
    str.Format("%3.1f", sSet.m_RH);
    m_ListCtrl.SetItemText( nItem, 4, str);
    str.Format("%3.1f", sSet.m_temp);
    m_ListCtrl.SetItemText( nItem, 5, str);

    nItem++;
    sSet.MoveNext();


    } sSet.Close();
    }
      

  2.   

    你说的是m_ListCtrl.SetRedraw(FALSE);
    上面代码
    m_ListCtrl.SetRedraw(TRUE);
          m_ListCtrl.Invalidate();
          m_ListCtrl.UpdateWindow();
      

  3.   

    是的,还要加一行删除所有items的代码。
      

  4.   

    m_ListCtrl.SetRedraw(FALSE);
    while (!sSet.IsEOF())
    {// m_ListCtrl.SetRedraw(FALSE);//这一句是放在这里 的话 listview就不显示了
    m_ListCtrl.InsertItem( nItem, sSet.m_address); // 插入学号
    //str=sSet.m_days.Format("%d-%m-%y"); 
    m_ListCtrl.SetItemText( nItem, 1,sSet.m_days);
    m_ListCtrl.SetItemText( nItem, 2,sSet.m_time);
    /*  int hour,mi,se;
    hour=sSet.m_days.GetHour();
    mi=sSet.m_days.GetMinute();
    se=sSet.m_days.GetSecond();
    str.Format("%02i:%02i:%02i", hour,mi,se);*/
    // str.Format("%4.1f", sSet.m_RH);

    str.Format("%3.1f", sSet.m_Votage);
    m_ListCtrl.SetItemText( nItem, 3, str);
    str.Format("%3.1f", sSet.m_RH);
    m_ListCtrl.SetItemText( nItem, 4, str);
    str.Format("%3.1f", sSet.m_temp);
    m_ListCtrl.SetItemText( nItem, 5, str);

    nItem++;
    sSet.MoveNext();


    }
    m_ListCtrl.SetRedraw(TRUE);
          m_ListCtrl.Invalidate();
          m_ListCtrl.UpdateWindow();我之前这样弄的不行的
      

  5.   

    那就将调用刷新的代码放在oncommand的最后,另外,access数据库有个特征,保存数据后将连
    接关闭一下再打开,否则取的数据是旧的。
      

  6.   

    sSet记录集重新取下数据。如果数据还是旧的,就关掉连接再重新打开连接。
      

  7.   

    其实你没有必要取数据库里的数据,从串口取的数据直接添加到listview 中显示就可以了,追加就行,当然你要显示很久以前的数据的话还是要用数据库的。
      

  8.   

    OnCommand()中1.添加数据到数据库2. 从数据库中取需要显示的数据。
    3.绑定到Listview.
      

  9.   

    我的OnCommand()是在chuankouview.cpp中执行的,执行的时候进行添加数据到数据库中
     而从数据库中取需要显示的数据是在 CExam9_1View.cpp中的 Display()进行的
      

  10.   

    感觉是在CExam9_1View.cpp中的对 Display()进行修改或者调用 不过我是没弄好 
      

  11.   

    access的问题  数据并不是实时更新的这样一个更改数据库      然后根据数据库显示 是不可取的    建议将数据记录在内存中 然后更新你的listview
      

  12.   

    我在串口触发函数oncom函数后面添加  GetDocument()->UpdateAllViews(NULL,1,NULL);并不能更新视图
      

  13.   

    解决了  是listview控件的更新函数没有执行 即 void CExam9_1View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
    函数没有执行解决的方法是 在你需要更新listview控件控件的地方调用GetDocument()->UpdateAllViews()函数 
     我这里调用GetDocument()->UpdateAllViews(NULL,1, (CObject*)new CString(str3));函数是在oncomm函数中 (是在执行完读取并保持串口数据到数据库后调用该函数来更新listview控件)
      

  14.   

    具体的是 :在oncomm函数中写入代码:
             CString str3;
    str3='2';
    GetDocument()->UpdateAllViews(NULL,1, (CObject*)new CString(str3))
             注意的是 这里的第二个参数是在listview中判断条件 这里可以任意写 只要前后一致就可以 
             第三个是 数据库查询条件 可以根据自己的查询条件来写,   
    在listview中的 OnUpdate函数代码如下:
                           void CExam9_1View::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
    {
    // TODO: Add your specialized code here and/or call the base class if(lHint==1)//这里就是和上面的UpdateAllViews
    {
         Display();

    CString string;
    string=*((CString *)pHint);
         CListCtrl& m_ListCtrl = GetListCtrl();
    m_ListCtrl.DeleteAllItems();

    Exam9_1Set sSet;
    int nItem = 0;
    CString str;
    sSet.m_strSort="time";
    sSet.m_strFilter.Format("address='%s'",string);
    sSet.Open();
      

  15.   

    (略去try,catch)
    先m_pConnection->Close
    再m_pConnection->Open