我把一个用ACCESS创建的数据库在对话框中用ListCtrl控件显示,现在想选中一条记录后,按“修改”按钮,弹出一个对话框,对话框可以看到目前这条记录的信息,在对话框中修改完成后,数据库和ListCtrl中的相应记录也修改。

解决方案 »

  1.   

    void CDBDialog::OnInfomodify() 
    {
    // TODO: Add your control notification handler code here
    UpdateData(TRUE);
    int nSelCount = m_listinfo.GetSelectedCount();
    if (nSelCount <= 0)
    {
    AfxMessageBox("请选择修改项!",MB_OK|MB_ICONEXCLAMATION);
            return;
    }; CDBInfoModifyDlg dlg;

    dlg.m_infoid = m_PatientInfoRecordset1.m_id;
    dlg.m_infoname = m_PatientInfoRecordset1.m_name;
             dlg.m_infosex = m_PatientInfoRecordset1.m_sex;
    dlg.m_infoage = m_PatientInfoRecordset1.m_age;
    dlg.m_infolr = m_PatientInfoRecordset1.m_lr ;
    UpdateData(FALSE);
    if(dlg.DoModal() != IDOK)
    return;
    try{
     
                    m_PatientInfoRecordset1.Edit();
           m_PatientInfoRecordset1.m_id = dlg.m_infoid;
           m_PatientInfoRecordset1.m_name = dlg.m_infoname;
           m_PatientInfoRecordset1.m_sex = dlg.m_infosex;
           m_PatientInfoRecordset1.m_age= dlg.m_infoage;
           m_PatientInfoRecordset1.m_lr = dlg.m_infolr ;
    if(m_PatientInfoRecordset1.CanUpdate())
    {
    m_PatientInfoRecordset1.Update();
    }
    }
    catch(CDaoException *exception)
    {
    exception->ReportError();
    m_PatientInfoRecordset1.MoveNext();
    m_PatientInfoRecordset1.MovePrev();
    return;
    }
        m_PatientInfoRecordset1.Update();
        m_PatientInfoRecordset.Close();
    m_PatientInfoRecordset.Open();
    ShowAllData();
    }我用这段代码,结果每次弹出的对话框都是显示的列表中的最后一条记录,而且也修改不成功。
    为什么呢?大家一定要帮我啊!!
      

  2.   

    CDBInfoModifyDlg dlg;
    m_PatientInfoRecordset1.Move(????);
    dlg.m_infoid = m_PatientInfoRecordset1.m_id;
    dlg.m_infoname = m_PatientInfoRecordset1.m_name;
             dlg.m_infosex = m_PatientInfoRecordset1.m_sex;
    dlg.m_infoage = m_PatientInfoRecordset1.m_age;
    dlg.m_infolr = m_PatientInfoRecordset1.m_lr ;
    UpdateData(FALSE);////////////////////去掉
    if(dlg.DoModal() != IDOK)
    return;
    try{
     
                    m_PatientInfoRecordset1.Edit();
           m_PatientInfoRecordset1.m_id = dlg.m_infoid;
           m_PatientInfoRecordset1.m_name = dlg.m_infoname;
           m_PatientInfoRecordset1.m_sex = dlg.m_infosex;
           m_PatientInfoRecordset1.m_age= dlg.m_infoage;
           m_PatientInfoRecordset1.m_lr = dlg.m_infolr ;
    //if(m_PatientInfoRecordset1.CanUpdate())
    //{
    m_PatientInfoRecordset1.Update();
    //}
    }
    catch(CDaoException *exception)
    {
    exception->ReportError();
    m_PatientInfoRecordset1.MoveNext();
    m_PatientInfoRecordset1.MovePrev();
    return;
    }
      

  3.   

    dfyang:我按照你的方法可以在对话框中显示记录信息,而且可以修改,可是并不是我选中的记录啊?!
    这是Move的那段代码,
    int nItem;
    POSITION pos = m_listinfo.GetFirstSelectedItemPosition();
    nItem = m_listinfo.GetNextSelectedItem(pos);m_PatientInfoRecordset1.MoveFirst();
     
    m_PatientInfoRecordset1.Move(nItem);
    我跟踪调试,也没有发现什么规律,不知道这段代码哪里有问题,哪位大侠一定要帮帮我这个菜鸟啊
      

  4.   

    不能直接使用clist中的序号来对应数据库中记录的顺序的(会乱的),最好是在每个item中记录下对应的记录的一些关键key,再用这些关键key来决定是对应哪一条记录,实现上比较复杂。
      

  5.   

    我在数据库里直接搜索,
              int nItem = GetSelecteListItem();//得到选中的行
    CString str = m_listinfo.GetItemText(nItem,0);
    CString strSql;
    strSql = "SELECT * FROM PatientInfo WHERE Id = '"+str+"'";
    m_PatientInfoRecordset1.Close();

        m_PatientInfoRecordset1.Open(AFX_DAO_USE_DEFAULT_TYPE,strSql,0);
    再接
    CDBInfoModifyDlg dlg;

    dlg.m_infoid = m_PatientInfoRecordset1.m_id;
    dlg.m_infoname = m_PatientInfoRecordset1.m_name;
             dlg.m_infosex = m_PatientInfoRecordset1.m_sex;
    dlg.m_infoage = m_PatientInfoRecordset1.m_age;
    dlg.m_infolr = m_PatientInfoRecordset1.m_lr ;。
    可是提示Too few parameters.Expected 1.,再点就终止调试了。这是我在一个例子里看到的,他是在同一个对话框里显示记录和修改记录,我就调用了一个对话框,就出问题了,为什么啊
      

  6.   

    int i=m_list.GetSelectionMark();
    if(i==-1)
         MessageBox("请选择选项!","请选择选项...",MB_OK|MB_ICONSTOP);
    else
    {你的操作}
    用下面的函数来确定你所选准的项
    int m_list.GetSelectionMark()函数的返回值是你所选准的行!!!!
      

  7.   

    然后用dlg.m_infoid =m_list.GetItemText(i,x);来给值x为列号