?? dbgrid.cpp
字號:
// DBGrid.cpp : implementation file
//
#include "stdafx.h"
#include "DBGrid.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// DBGrid
DBGrid::DBGrid()
{
}
DBGrid::~DBGrid()
{
}
bool DBGrid::SetRecordset(_RecordsetPtr set)
{
// 初始化列表視圖
InitCtrlList();
// 根據記錄集字段數目插入相應的列
CString sFieldName;
for (int i=0; i<set->GetFields()->GetCount(); i++)
{
sFieldName = (char*)(_bstr_t)(set->GetFields()->GetItem((long)i)->GetName());
this->InsertColumn(i, sFieldName);
}
i = 0;
set->MoveFirst();
while(!set->EndOfFile) // 循環插入每一行(一行一條記錄)
{
this->InsertItem(i, "");
for (int j=0; j<set->GetFields()->GetCount(); j++) // 循環設置每一行的每個字段值
{
// 先獲取字段名稱
sFieldName = (char*)(_bstr_t)(set->GetFields()->GetItem((long)j)->GetName());
// 再根據字段名稱獲取字段值
_variant_t Holder;
Holder = set->GetCollect((_bstr_t)sFieldName);
CString str = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
this->SetItemText(i,j, str);
// 注:
// 如果不先將結果保存至 _variant_t 對象有可能會使程序掛掉
// 因為有些對象是不能直接轉換為 (char*)(_bstr_t) 的,但是 _variant_t
// 可以將所有對象轉化為 (char*)(_bstr_t),所以先將結果保存至 _variant_t
}
i++;
set->MoveNext();
}
// 自動調整各列的寬度
for (i=0; i<set->GetFields()->GetCount(); i++)
{
this->SetColumnWidth(i, LVSCW_AUTOSIZE_USEHEADER );
}
return true;
}
bool DBGrid::InitCtrlList()
{
// Set Color
this->SetBkColor(RGB(177, 151, 240));
this->SetTextColor(RGB(0,0,0));
this->SetTextBkColor(RGB(177, 151, 240));
// Set EX-Style
this->SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_HEADERDRAGDROP);
// 刪除所有行
this->DeleteAllItems();
// 刪除所有列
LVCOLUMN column;
while(this->GetColumn(0, &column))
{
this->DeleteColumn(0);
}
return true;
}
BEGIN_MESSAGE_MAP(DBGrid, CListCtrl)
//{{AFX_MSG_MAP(DBGrid)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// DBGrid message handlers
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -