// Eg10_1View.cpp : implementation of the CEg10_1View class
//

#include "stdafx.h"
#include "Eg10_1.h"

#include "Eg10_1Set.h"
#include "Eg10_1Doc.h"
#include "Eg10_1View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEg10_1View

IMPLEMENT_DYNCREATE(CEg10_1View, CRecordView)

BEGIN_MESSAGE_MAP(CEg10_1View, CRecordView)
	//{{AFX_MSG_MAP(CEg10_1View)
	ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
	ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CEg10_1View construction/destruction

CEg10_1View::CEg10_1View()
	: CRecordView(CEg10_1View::IDD)
{
	//{{AFX_DATA_INIT(CEg10_1View)
	m_pSet = NULL;
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_bAddMode=FALSE;

}

CEg10_1View::~CEg10_1View()
{
}

void CEg10_1View::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEg10_1View)
	DDX_FieldText(pDX, IDC_ENGLISH, m_pSet->m_English, m_pSet);
	DDX_FieldText(pDX, IDC_MATHS, m_pSet->m_Maths, m_pSet);
	DDX_FieldText(pDX, IDC_NAME, m_pSet->m_Name, m_pSet);
	DDX_FieldText(pDX, IDC_STUNO, m_pSet->m_Stuno, m_pSet);
	//}}AFX_DATA_MAP
}

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

	return CRecordView::PreCreateWindow(cs);
}

void CEg10_1View::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_eg10_1Set;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CEg10_1View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CEg10_1View diagnostics

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

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

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

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


/////////////////////////////////////////////////////////////////////////////
// CEg10_1View message handlers

void CEg10_1View::OnRecordAdd() 
{
	// TODO: Add your command handler code here
	m_pSet->AddNew();
    m_bAddMode=TRUE;
	CEdit *pCtrl=(CEdit *)GetDlgItem(IDC_STUNO);
	int result=pCtrl->SetReadOnly (FALSE);
	UpdateData(FALSE);
	
}

void CEg10_1View::OnRecordDelete() 

{
	// TODO: Add your command handler code here
		m_pSet->Delete();
		m_pSet->MoveNext();
		if(	m_pSet->IsEOF())
			m_pSet->MoveLast();
		if(	m_pSet->IsBOF())
			m_pSet->SetFieldNull(NULL);
		UpdateData(FALSE);

	
}

BOOL CEg10_1View::OnMove(UINT nIDMoveCommand) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(m_bAddMode)
	{
		m_bAddMode=FALSE;
	    UpdateData(TRUE);
	    if(!m_pSet->CanUpdate())
			return FALSE;
		TRY
		{
		     m_pSet->Update();
		}
		CATCH(CDBException,e)
		{
			AfxMessageBox(e->m_strError);
			return FALSE;
		}
		END_CATCH;

	     m_pSet->Requery();	
         UpdateData(FALSE);
         CEdit *pCtrl=(CEdit *)GetDlgItem(IDC_STUNO);
	     pCtrl->SetReadOnly (TRUE);
	     return TRUE;
	}
	
	return CRecordView::OnMove(nIDMoveCommand);
}
