www.gusucode.com > VC++动态刷新数据库和多表切换示例-源码程序 > VC++动态刷新数据库和多表切换示例-源码程序/code/mytest/mytestView.cpp

    //Download by http://www.NewXing.com
// mytestView.cpp : implementation of the CMytestView class
//

#include "stdafx.h"
#include "mytest.h"

#include "mytestSet.h"
#include "mytestDoc.h"
#include "mytestView.h"
#include "zyxxset.h"
#include "zyxxView.h"
#include "dialogfilter.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMytestView

IMPLEMENT_DYNCREATE(CMytestView, CRecordView)

BEGIN_MESSAGE_MAP(CMytestView, CRecordView)
	//{{AFX_MSG_MAP(CMytestView)
	ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
	ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
	ON_COMMAND(ID_RECORD_FIRST, OnRecordFirst)
	ON_COMMAND(ID_RECORD_LAST, OnRecordLast)
	ON_COMMAND(ID_RECORD_NEXT, OnRecordNext)
	ON_COMMAND(ID_RECORD_PREV, OnRecordPrev)
	ON_COMMAND(ID_RECORD_SAVE, OnRecordSave)
	ON_COMMAND(ID_SORT_COUNT, OnSortCount)
	ON_COMMAND(ID_SORT_ID, OnSortId)
	ON_COMMAND(ID_SORT_BIRTHDAY, OnSortBirthday)
	ON_CBN_DROPDOWN(IDC_SPECIALITY, OnDropdownSpeciality)
	ON_COMMAND(ID_FILTER, OnFilter)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMytestView construction/destruction

CMytestView::CMytestView()
	: CRecordView(CMytestView::IDD)
{
	//{{AFX_DATA_INIT(CMytestView)
	m_pSet = NULL;
	m_count = 0;
	m_total_number = 0;
	m_total_id = 0;
    m_pSet1=NULL;
	//}}AFX_DATA_INIT
	// TODO: add construction code here
   m_bAdd = false;
}

CMytestView::~CMytestView()
{
}

void CMytestView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMytestView)
	DDX_Control(pDX, IDC_SPECIALITY, m_ctlType);
	DDX_Control(pDX, IDC_ID, m_ctlNo);
	DDX_Text(pDX, IDC_EDIT6, m_count);
	DDX_Text(pDX, IDC_TOTAL_NUMBER, m_total_number);
	DDX_Text(pDX, IDC_TOTAL_ID, m_total_id);
	DDX_DateTimeCtrl(pDX, IDC_BIRTHDAY, m_pSet->m_birthday);
	DDX_FieldText(pDX, IDC_ENGLISH, m_pSet->m_english, m_pSet);
	DDX_FieldText(pDX, IDC_ID, m_pSet->m_ID, m_pSet);
	DDX_FieldText(pDX, IDC_MATH, m_pSet->m_math, m_pSet);
	DDX_FieldText(pDX, IDC_MEMO, m_pSet->m_memo, m_pSet);
	DDX_FieldText(pDX, IDC_NAME, m_pSet->m_name, m_pSet);
	DDX_FieldText(pDX, IDC_PHYSICS, m_pSet->m_physics, m_pSet);
	DDX_FieldCBString(pDX, IDC_SEX, m_pSet->m_sex, m_pSet);
	DDX_FieldCBString(pDX, IDC_SPECIALITY, m_pSet->m_speciality, m_pSet);
	DDX_FieldCheck(pDX, IDC_PARTY, m_pSet->m_partymember, m_pSet);
	//}}AFX_DATA_MAP
}

BOOL CMytestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRecordView::PreCreateWindow(cs);
}

void CMytestView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_mytestSet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
    CMytestSet myset;
	myset.Open();
	nRecordNumber=0;
	while(!myset.IsEOF())
	{
		nRecordNumber++;
		myset.MoveNext();
	}
	myset.Close();
	m_total_number = nRecordNumber;
	m_count = m_pSet->m_english+m_pSet->m_math+m_pSet->m_physics;
    m_total_id=1;
	UpdateData(FALSE);
	//
}

/////////////////////////////////////////////////////////////////////////////
// CMytestView printing

BOOL CMytestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMytestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMytestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMytestView diagnostics

#ifdef _DEBUG
void CMytestView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CMytestView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

CMytestDoc* CMytestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMytestDoc)));
	return (CMytestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMytestView database support
CRecordset* CMytestView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CMytestView message handlers

void CMytestView::OnRecordAdd() 
{
	// TODO: Add your command handler code here
    m_pSet->AddNew();
    m_bAdd = true;  
	CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_ID);
	int result = pCtrl->SetReadOnly(FALSE);
	m_count =0;
	UpdateData(FALSE);
}

void CMytestView::OnRecordDelete() 
{
	// TODO: Add your command handler code here
	m_pSet->Delete();
	m_pSet->Requery();
	UpdateData(false);

}

void CMytestView::OnRecordFirst() 
{
	// TODO: Add your command handler code here
	m_pSet->MoveFirst();
     m_count = m_pSet->m_english+m_pSet->m_math+m_pSet->m_physics;
     m_total_id=1;
	 UpdateData(FALSE);
}

void CMytestView::OnRecordLast() 
{
	// TODO: Add your command handler code here
	m_pSet->MoveLast();
  m_count = m_pSet->m_english+m_pSet->m_math+m_pSet->m_physics;
    m_total_id= nRecordNumber; 
  UpdateData(FALSE);
}

void CMytestView::OnRecordNext() 
{
	// TODO: Add your command handler code here
	if(!m_pSet->IsEOF())
	{
	m_pSet->MoveNext();
    m_count = m_pSet->m_english+m_pSet->m_math+m_pSet->m_physics;
    m_total_id++;   
    UpdateData(FALSE);
	}
	else
	{
		m_total_id--;
	}
}

void CMytestView::OnRecordPrev() 
{
	// TODO: Add your command handler code here
	m_pSet->MovePrev();
    m_count = m_pSet->m_english+m_pSet->m_math+m_pSet->m_physics;
    m_total_id--;  
    UpdateData(FALSE);

}

void CMytestView::OnRecordSave() 
{
	// TODO: Add your command handler code here
	UpdateData(true);
    //用控件值更新成员变量;
	m_pSet->Update();
	CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_ID);
	int result = pCtrl->SetReadOnly(TRUE);
}

void CMytestView::OnSortCount() 
{
	// TODO: Add your command handler code here
    m_pSet->Close();
	m_pSet->m_strSort="math+english+physics";
    m_pSet->Open();
	UpdateData(false);
	
}

void CMytestView::OnSortId() 
{
	// TODO: Add your command handler code here
	m_pSet->Close();
	m_pSet->m_strSort="ID";
    m_pSet->Open();
	UpdateData(false);
}

void CMytestView::OnSortBirthday() 
{
	// TODO: Add your command handler code here
	m_pSet->Close();
	m_pSet->m_strSort="birthday";
    m_pSet->Open();
	UpdateData(false);
}
void CMytestView::OnDropdownSpeciality() 
{
	// TODO: Add your control notification handler code here
	CMytestDoc *pDoc = (CMytestDoc *)GetDocument();
	m_pSet1 = &pDoc->m_ZyxxSet;
	m_pSet1->m_strSort = "sp_ID";
	m_pSet1->m_pDatabase = pDoc->GetDatabase();
	
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	CZyxxSet cZyxxSet;
	if(cZyxxSet.m_pDatabase == NULL)
		cZyxxSet.m_pDatabase = pDoc->GetDatabase();
	try
	{
		cZyxxSet.Open();
	}
	catch (CDBException *e)
	{
		AfxMessageBox(e->m_strError);
		e->Delete();
		return;
	}
    // Fill in the type Of product
	m_ctlType.ResetContent();
	if (cZyxxSet.IsOpen())
	{
		while (!cZyxxSet.IsEOF())
		{
			m_ctlType.AddString(cZyxxSet.m_sp_name);
			cZyxxSet.MoveNext();
		}
	}
	m_ctlType.SetCurSel(0);
	cZyxxSet.Close();
}
BOOL CMytestView::OnMove(UINT nIDMoveCommand) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(m_bAdd)
	{
		m_bAdd = false;
		UpdateData(true);
		if(m_pSet->CanAppend())
			m_pSet->Update();
		m_pSet->Requery();
		UpdateData(false);
		CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_ID);
	int result = pCtrl->SetReadOnly(TRUE);
		return true;
	}
	return CRecordView::OnMove(nIDMoveCommand);
}

void CMytestView::OnFilter() 
{
	// TODO: Add your command handler code here
	CDialogFilter dlg;
	dlg.DoModal();
	CString value;
	nRadio = dlg.Radio;
	value = dlg.m_filter_value;
 if(nRadio == "Sex")
	{
		MessageBox("查询性别","查询");
        m_pSet->Close();
	    m_pSet->m_strFilter = "sex='"+value+"'";
       //dlg.m_pSet->m_strSort = "ID";
		//dlg.m_pSet->m_strFilter.Format("sex='%s'",dlg.m_filter_value);
    	m_pSet->Open();
		m_pSet->Requery();
	    UpdateData(false);
    }
 else if (nRadio == "Name")
 {
      m_pSet->Close();
	  m_pSet->m_strFilter = "name='"+value+"'";
	  m_pSet->Open();
	  m_pSet->Requery();
	    UpdateData(false);
	 MessageBox("查询姓名","查询");
 }
 else if (nRadio == "Party")
 {
	  m_pSet->Close();
	 if(dlg.m_filter_value == "是")
		  m_pSet->m_strFilter = "partymember =true";
	 else
		 m_pSet->m_strFilter = "partymember =false";
	 m_pSet->Open();
	    m_pSet->Requery();
	    UpdateData(false);
MessageBox("查询是否党员","查询");
 }
 else
 {
	 MessageBox("请选择","错误");
 }
}