📄 listdoc.cpp
字号:
// ListDoc.cpp: implementation of the CListDoc class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ListDoc.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CListDoc::CListDoc()
{
m_pListCtrl = NULL;
m_nAutoKey = -1;
}
CListDoc::~CListDoc()
{
m_caKey.RemoveAll();
}
/////////////////////////////////////////////////
//初始化
//pDlg 指定初始化窗口
//pCtrl 指定列表控件
BOOL CListDoc::Init(CDialog *pDlg, CListCtrlBase *pCtrl)
/////////////////////////////////////////////////
{
if (pDlg == NULL) return FALSE;
CPreDialog PreDlg;
m_data.clear();
m_pDlg = pDlg;
PreDlg.Enum(pDlg); //枚举窗口结构
Init4Control(&PreDlg);
SetListCtrl(pCtrl);
InitList();
return TRUE;
}
void CListDoc::RetrieveA(){}; //快速查找Retrieve()
int CListDoc::Retrieve(CString strSQL,
BOOL bMsg,
long lOption,
CursorTypeEnum CursorType,
LockTypeEnum LockType)
{
CDlgDataCell Cell;
CString strText;
// int nItem, i, nSubItem;
//清除
if(m_pListCtrl == NULL)return -1;
m_bModify = false;
m_nAutoKey = -1;
m_caKey.RemoveAll();
m_pListCtrl->SetRedraw(FALSE);
m_pListCtrl->DeleteAllItems();
//////////////////////////////
if (!m_RecordSet.Open(strSQL,lOption, CursorType, LockType))
{
m_pListCtrl->SetRedraw(true);
if (bMsg)
::MessageBox(NULL,"错误:错误位置"+strText, "系统提示", MB_OK|MB_ICONEXCLAMATION);
return -1;
}
if (m_RecordSet.IsBOF() || m_RecordSet.IsEOF())
{
m_pListCtrl->SetRedraw(true);
if (bMsg)
::MessageBox(NULL,"注意:没有发现任何资料!","系统提示", MB_OK|MB_ICONEXCLAMATION);
return 100;
}
//导入
while(m_RecordSet.IsEOF() == FALSE)
{
Db2List(&m_RecordSet);
m_RecordSet.MoveNext();
}//end while
m_pListCtrl->SetRedraw(TRUE);
return 0;
}
///////////////////////////////////////////////////
//从控件导入导出到ReportList
//bFlog 导出、导入
//iCol 指定导入导出位置CLD_BEGIN、CLD_END、CLD_CURROR
//bMsg 显示
// 默认为当前行
BOOL CListDoc::UpdateData(BOOL bFlog,int iCol, BOOL bMsg)
///////////////////////////////////////////////////
{
int i, nSubItem, iSel;
// char buf[1025];
CDlgDataCell Cell;
CString strText;
//当前光标
if (m_pListCtrl==NULL) return FALSE;
switch(iCol)
{
case CLD_BEGIN: //首行
iSel = 0;
break;
case CLD_END: //末行
iSel = m_pListCtrl->GetItemCount( )-1;
break;
case CLD_CURSOR: //当前行
iSel = _GetSelected();
break;
}
if (iSel < 0) return FALSE;
// if (!_MoveSelect(iSel)) return false;
//导出数据
for (i=0; i < m_data.size(); i++)
{
Cell = m_data.at(i);
if (Cell.dwStyle & CDD_RETRIEVE)
{
//if(Cell.iIndexCol < 0)
// nSubItem = m_pListCtrl->GetSubItem(Cell.strText);
//else
nSubItem = Cell.iIndexCol;
if (nSubItem < 0) continue;
if (bFlog)
{//导出
//m_pListCtrl->GetItemText(iSel, nSubItem, buf, 1024);
//strText = buf;
strText = m_pListCtrl->GetItemText(iSel, nSubItem);
SetWindowTextEx(&Cell, strText);
}
else
{//导入
if (Cell.dwStyle & CDD_UPDATE)
{
strText = GetWindowTextEx(&Cell);
m_pListCtrl->SetItemText(iSel, nSubItem, strText);
//if (!m_RecordSet.PutCollect(Cell.strName, strText))
// return false;
if(GetModify(iSel) == LD_NONE)
SetModify(iSel, LD_EDIT);
m_bModify = true;
}
}
}//end if
}//end for
// if (bFlog) return true;
//保存
// if(!m_RecordSet.Update())
// {
// if (bMsg)
// ::MessageBox(NULL,"保存:保存资料失败!","系统提示", MB_OK|MB_ICONEXCLAMATION);
// TRACE(_T("错误:新增数据失败!"));
// return false;
// }
//更新显示
/* for (i=0; i < m_data.size(); i++)
{
Cell = m_data.at(i);
if (Cell.dwStyle & CDD_RETRIEVE)
{
if(Cell.iIndexCol < 0)
nSubItem = m_pListCtrl->GetSubItem(Cell.strText);
else
nSubItem = Cell.iIndexCol;
if (nSubItem < 0) continue;
if (Cell.dwStyle & CDD_UPDATE)
{
strText = GetWindowTextEx(&Cell);
m_pListCtrl->SetItemText(iSel, nSubItem, strText);
}
}//end if
}//end for
*/
return true;
}
//////////////////////////////////////////////////////
//用主键新增一列
//bMsg 是否显示
//pKey 主键 如果主键为NULL表示自动AutoKey
void CListDoc::NewA(){};
BOOL CListDoc::New(BOOL bMsg, LPCSTR pKey)
//////////////////////////////////////////////////////
{
int nItem, i, nSubItem;
CDlgDataCell Cell;
CString sKey;
if (m_pListCtrl==NULL)return false;
//if(!m_RecordSet.AddNew()) goto err01;
sKey = pKey;
if (pKey == NULL) //自动生成主键
sKey.Format("%d", --m_nAutoKey);
else if(_CheckKey(sKey) == false)
goto err01; //主键重复
nItem = _GetSelected();
if(nItem < 0) nItem = 0;
nItem = m_pListCtrl->InsertItem(nItem,_T(""));
for (i=0; i < m_data.size(); i++)
{
Cell = m_data.at(i);
if (Cell.dwStyle & CDD_RETRIEVE)
{
nSubItem = Cell.iIndexCol;
if(m_strKey == Cell.strName)
{//主键
m_pListCtrl->SetItemText(nItem, nSubItem, sKey);
}
else
{
m_pListCtrl->SetItemText(nItem, nSubItem,
Cell.strDefText);
}
}//end if
}//end for
//更新显示
m_pListCtrl->SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
SetModify(nItem, LD_NEW);
//m_bModify = true;
return true;
err01:
if (bMsg)
::MessageBox(NULL,"错误:新增数据失败!","系统提示", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
int CListDoc::Delete(BOOL bCon, BOOL bMsg)
{
CString sKey, sSql;
int nItem, nSubItem;
//无ListCtrl
if(m_pListCtrl == NULL)return -1;
//无主键
if(m_strKey == _T("")) goto err02;
//提示
if(bCon && (::MessageBox(NULL,"注意:你确定要删除数据吗?","系统提示", MB_OKCANCEL|MB_ICONEXCLAMATION) != IDOK))
return 100;
//返回当前项的主键值
nItem = _GetSelected();
nSubItem = _GetSubItem(m_strText);
//sKey = m_pListCtrl->GetItemTextEx(nItem, m_strText);
sKey = m_pListCtrl->GetItemText(nItem, nSubItem);
if (sKey == _T("")) goto err03;
m_caKey.Add(sKey);
// if(!_MoveSelect(nItem)) goto err03;
// if(!m_RecordSet.Delete()) goto err03;
//更新显示
m_pListCtrl->DeleteItem(nItem);
nItem <= 0? 0 : nItem-1;
m_pListCtrl->SetItemState(nItem, LVIS_SELECTED, LVIS_SELECTED);
// m_pListCtrl->SelectItem(nItem);
m_bModify = true;
return 0;
err02:
::MessageBox(NULL,"错误:主键不能为空!","系统提示", MB_OK|MB_ICONEXCLAMATION);
return -1;
err03:
if (bMsg)
::MessageBox(NULL,"错误:删除数据失败!","系统提示", MB_OK|MB_ICONEXCLAMATION);
return -1;
}
/////////////////////////////////////////////
//删除所有记录
int CListDoc::DeleteAll(BOOL bCon, BOOL bMsg)
{
CString sKey, sSql;
if(m_pListCtrl == NULL)return -1;
if(!m_RecordSet.IsOpen()) goto err02;
if(bCon && (::MessageBox(NULL,"注意:你确定要删除数据吗?","系统提示", MB_OKCANCEL|MB_ICONEXCLAMATION) != IDOK))
return 100;
//删除所有
if(!m_RecordSet.MoveFirst()) return 0;
m_pConnection->BeginTrans();
while(!m_RecordSet.IsEOF())
{
if(!m_RecordSet.Delete()) goto err03;
m_RecordSet.MoveNext();
}
m_pConnection->CommitTrans();
// if(!m_RecordSet.Delete(adAffectAll)) goto err03;
// return Save(false);
return 0;
err02:
m_pConnection->RollbackTrans();
::MessageBox(NULL,"错误:没有指定删除对象!","系统提示", MB_OK|MB_ICONEXCLAMATION);
return -1;
err03:
if (bMsg)
::MessageBox(NULL,"错误:删除数据失败!","系统提示", MB_OK|MB_ICONEXCLAMATION);
return -1;
}
///////////////////////////////////////////////
//保存数据
BOOL CListDoc::Save(BOOL bTrans, BOOL bMsg)
///////////////////////////////////////////////
{
/* if(m_bModify == false)return true;
if (m_RecordSet.Update())
{
Cancel();
if (bMsg)
::MessageBox(NULL,"注意:保存资料失败!","系统提示", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
*/
CString sKey, sText;
int i,j, nData, nSubItem;
CDlgDataCell Cell;
if(bTrans) m_pConnection->BeginTrans();
//删除
int nRet = m_RecordSet.GetFieldType(m_strKey);
for (i = 0; i < m_caKey.GetSize(); i++)
{
sKey = m_caKey.GetAt(i);
if(!_MoveSelect(sKey, nRet)) goto err01;
if(!m_RecordSet.Delete()) goto err01;
}
//新增
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -