FinanceDBGrid.cpp
上传用户:xiaoke98
上传日期:2014-06-29
资源大小:5718k
文件大小:4k
源码类别:

家庭/个人应用

开发平台:

Visual C++

  1. // FinanceDBGrid.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "HomeFinanceManager.h"
  5. #include "FinanceDBGrid.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CFinanceDBGrid
  13. CFinanceDBGrid::CFinanceDBGrid()
  14. {
  15. m_iColumnIndex = -1;
  16. m_Recordset = 0;
  17. }
  18. CFinanceDBGrid::~CFinanceDBGrid()
  19. {
  20. }
  21. BEGIN_MESSAGE_MAP(CFinanceDBGrid, CListCtrl)
  22. //{{AFX_MSG_MAP(CFinanceDBGrid)
  23. // NOTE - the ClassWizard will add and remove mapping macros here.
  24. //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CFinanceDBGrid message handlers
  28. void CFinanceDBGrid::setRecordSet(_RecordsetPtr pRecordSet)
  29. {
  30. m_Recordset = pRecordSet;
  31. }
  32. void CFinanceDBGrid::Reflesh(void)
  33. {
  34. if(0 == m_Recordset)
  35. {
  36. return;
  37. }
  38. RECT rcClient;
  39. this->GetClientRect(&rcClient);
  40. int iClientWidth = rcClient.right - rcClient.left;
  41. long FieldsCount = m_Recordset->Fields->GetCount();
  42. int iWidthPerDiv = iClientWidth / FieldsCount;
  43. if(FieldsCount)
  44. {
  45. int iColumNum = GetHeaderCtrl()->GetItemCount();
  46. int i;
  47. for(i = 0; i < iColumNum; i++)
  48. {
  49. DeleteColumn(0);
  50. }
  51. for(i = 0; i < FieldsCount; i++)
  52. {
  53. CString strFieldName;
  54. FieldsPtr TempFields = m_Recordset->GetFields();
  55. FieldPtr TempField = TempFields->Item[(long)i];
  56. strFieldName = (char*)(TempField->GetName());
  57. addColumn(strFieldName.GetBuffer(0), iWidthPerDiv);
  58. }
  59. }
  60. long recordCount = m_Recordset->GetRecordCount();
  61. DeleteAllItems();
  62. if(recordCount)
  63. {
  64. int iPos = -1;
  65. m_Recordset->MoveFirst();
  66. while(!m_Recordset->adoEOF)
  67. {
  68. LV_ITEM lvitem;
  69. lvitem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
  70. lvitem.state = 0;      
  71. lvitem.stateMask = 0; 
  72. CString strTemp;
  73. _variant_t Value;
  74. lvitem.iItem = ++iPos;
  75. lvitem.iSubItem = 0;
  76. Value = m_Recordset->GetCollect((long)0);
  77. FormateVariant(Value, strTemp);
  78. lvitem.pszText = strTemp.GetBuffer(0);
  79. InsertItem(&lvitem);
  80. for(int iCol = 1; iCol < FieldsCount; iCol++)
  81. {
  82. Value = m_Recordset->GetCollect((long)iCol);
  83. FormateVariant(Value, strTemp);
  84. SetItemText(iPos, iCol, (LPCTSTR)strTemp.GetBuffer(0));
  85. }
  86. m_Recordset->MoveNext();
  87. }
  88. }
  89. }
  90. void CFinanceDBGrid::addColumn(char* strColumnName, int iColumnWidth)
  91. {
  92. LONG lStyle = this->SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
  93. lStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP;
  94. this->SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)lStyle);
  95. LV_COLUMN lvc;
  96. lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH /*| LVCF_FMT*/;
  97. m_iColumnIndex++;
  98. lvc.iSubItem = m_iColumnIndex;
  99. lvc.pszText = strColumnName;
  100. lvc.cx = iColumnWidth;
  101. InsertColumn(m_iColumnIndex,&lvc);
  102. }
  103. void CFinanceDBGrid::FormateVariant(_variant_t value, CString& strValue)
  104. {
  105. COleCurrency var_currency;
  106. switch(value.vt)
  107. {
  108. case VT_CY:
  109. var_currency = value.cyVal;
  110. strValue = var_currency.Format(0);
  111. break;
  112. case VT_I4:
  113. strValue.Format("%d", value.lVal);
  114. break;
  115. case VT_BSTR:
  116. strValue = CString(value.bstrVal);
  117. break;
  118. case VT_DATE:
  119. unsigned short usDate;
  120. unsigned short usTime;
  121. VariantTimeToDosDateTime(value.date, &usDate, &usTime);
  122. CTime TempTime(usDate, usTime);
  123. strValue.Format("%d-%d-%d",TempTime.GetYear(), TempTime.GetMonth(), TempTime.GetDay());
  124. }
  125. }