FinanceDBGrid.cpp
上传用户:xiaoke98
上传日期:2014-06-29
资源大小:5718k
文件大小:4k
- // FinanceDBGrid.cpp : implementation file
- //
- #include "stdafx.h"
- #include "HomeFinanceManager.h"
- #include "FinanceDBGrid.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CFinanceDBGrid
- CFinanceDBGrid::CFinanceDBGrid()
- {
- m_iColumnIndex = -1;
- m_Recordset = 0;
- }
- CFinanceDBGrid::~CFinanceDBGrid()
- {
- }
- BEGIN_MESSAGE_MAP(CFinanceDBGrid, CListCtrl)
- //{{AFX_MSG_MAP(CFinanceDBGrid)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFinanceDBGrid message handlers
- void CFinanceDBGrid::setRecordSet(_RecordsetPtr pRecordSet)
- {
- m_Recordset = pRecordSet;
- }
- void CFinanceDBGrid::Reflesh(void)
- {
-
- if(0 == m_Recordset)
- {
- return;
- }
- RECT rcClient;
- this->GetClientRect(&rcClient);
- int iClientWidth = rcClient.right - rcClient.left;
- long FieldsCount = m_Recordset->Fields->GetCount();
- int iWidthPerDiv = iClientWidth / FieldsCount;
- if(FieldsCount)
- {
- int iColumNum = GetHeaderCtrl()->GetItemCount();
- int i;
- for(i = 0; i < iColumNum; i++)
- {
- DeleteColumn(0);
- }
-
- for(i = 0; i < FieldsCount; i++)
- {
- CString strFieldName;
- FieldsPtr TempFields = m_Recordset->GetFields();
- FieldPtr TempField = TempFields->Item[(long)i];
- strFieldName = (char*)(TempField->GetName());
- addColumn(strFieldName.GetBuffer(0), iWidthPerDiv);
- }
- }
-
- long recordCount = m_Recordset->GetRecordCount();
- DeleteAllItems();
- if(recordCount)
- {
-
- int iPos = -1;
-
- m_Recordset->MoveFirst();
- while(!m_Recordset->adoEOF)
- {
-
- LV_ITEM lvitem;
- lvitem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
- lvitem.state = 0;
- lvitem.stateMask = 0;
-
- CString strTemp;
- _variant_t Value;
-
-
- lvitem.iItem = ++iPos;
- lvitem.iSubItem = 0;
-
-
- Value = m_Recordset->GetCollect((long)0);
- FormateVariant(Value, strTemp);
- lvitem.pszText = strTemp.GetBuffer(0);
- InsertItem(&lvitem);
- for(int iCol = 1; iCol < FieldsCount; iCol++)
- {
- Value = m_Recordset->GetCollect((long)iCol);
- FormateVariant(Value, strTemp);
- SetItemText(iPos, iCol, (LPCTSTR)strTemp.GetBuffer(0));
- }
-
-
- m_Recordset->MoveNext();
- }
-
- }
- }
- void CFinanceDBGrid::addColumn(char* strColumnName, int iColumnWidth)
- {
- LONG lStyle = this->SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
- lStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP;
- this->SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)lStyle);
- LV_COLUMN lvc;
- lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH /*| LVCF_FMT*/;
-
- m_iColumnIndex++;
- lvc.iSubItem = m_iColumnIndex;
- lvc.pszText = strColumnName;
- lvc.cx = iColumnWidth;
- InsertColumn(m_iColumnIndex,&lvc);
- }
- void CFinanceDBGrid::FormateVariant(_variant_t value, CString& strValue)
- {
- COleCurrency var_currency;
- switch(value.vt)
- {
- case VT_CY:
- var_currency = value.cyVal;
- strValue = var_currency.Format(0);
- break;
- case VT_I4:
- strValue.Format("%d", value.lVal);
- break;
- case VT_BSTR:
- strValue = CString(value.bstrVal);
- break;
- case VT_DATE:
- unsigned short usDate;
- unsigned short usTime;
- VariantTimeToDosDateTime(value.date, &usDate, &usTime);
- CTime TempTime(usDate, usTime);
- strValue.Format("%d-%d-%d",TempTime.GetYear(), TempTime.GetMonth(), TempTime.GetDay());
- }
- }