CGridCtrl类的用法
作者:互联网
在VC6里用CGridCtrl遇到了如果添加Custome Ctrol这个控件的话,出现的列表无法响应鼠标点击事件,所以无法修改,后经百度,发现用Picture控件可以编辑。
Picture控件拖拽到界面上,定位无所谓,大小也无所谓,反正都是由程序来控制的,关键代码如下:
CRect rect;
GetClientRect(rect);
m_gridCtrl.Create(CRect(0, 0, rect.Width(), rect.Height() - 40), this, IDC_GRID);
关键是Create的代码,里面的IDC_GRID是Picture控件的ID。不必关联变量,而m_gridCtrl无疑是对话框的一个成员变量:CGridCtrl m_gridCtrl;
填充数据我使用了如下代码:
void COdbcgridDlg::ListStuInfo()
{
CRect gridRect;
m_gridCtrl.GetWindowRect(&gridRect);
int col = 0;
m_gridCtrl.DeleteAllItems(); //首先删除所有项目
col = m_gridCtrl.InsertColumn("ID", DT_LEFT|DT_VCENTER|DT_SINGLELINE);
m_gridCtrl.SetColumnWidth(col, 50);
col = m_gridCtrl.InsertColumn("姓名", DT_LEFT|DT_VCENTER|DT_SINGLELINE);
m_gridCtrl.SetColumnWidth(col, gridRect.Width() - (50 + 100 + 40) - 6);
col = m_gridCtrl.InsertColumn("性别", DT_LEFT|DT_VCENTER|DT_SINGLELINE);
m_gridCtrl.SetColumnWidth(col, 100);
col = m_gridCtrl.InsertColumn("分数", DT_RIGHT|DT_VCENTER|DT_SINGLELINE);
m_gridCtrl.SetColumnWidth(col, 40);
m_gridCtrl.SetFixedRowCount(1);
CInfoSet Infoset; //创建一个记录集对象
if(!Infoset.Open(CRecordset::dynaset, m_defaultSQL, CRecordset::readOnly))//连接数据源并执行给定的SQL语句
{
MessageBox("连接数据源失败!","警告",MB_ICONASTERISK|MB_OK);
return;
}
int i = 0;
GV_ITEM Item;
Item.mask = GVIF_TEXT|GVIF_FORMAT;
while(!Infoset.IsEOF())
{
Item.nFormat = DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
Item.row = m_gridCtrl.InsertRow(Item.strText);
Item.col = 0;
Item.strText.Format("%ld", Infoset.m_id);
m_gridCtrl.SetItem(&Item);
Item.col = 1;
Item.strText = Infoset.m_name;
m_gridCtrl.SetItem(&Item);
Item.col = 2;
Item.strText = Infoset.m_sex;
m_gridCtrl.SetItem(&Item);
Item.nFormat = DT_RIGHT|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
Item.col = 3;
Item.strText.Format("%s", Infoset.m_score);
m_gridCtrl.SetItem(&Item);
Infoset.MoveNext();
i++;
}
m_count.Format("%d", i);
Infoset.Close(); //关闭记录集
UpdateData(FALSE); //将控件变量值传给对应控件显示
}
CInfoSet 类是MFC创建的继承CRecordset的类,这是另一个课题了,见MFC ODBC用法。
源码粘贴如下:
// InfoSet.cpp : implementation file
//
#include "stdafx.h"
#include "odbcgrid.h"
#include "InfoSet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CInfoSet
IMPLEMENT_DYNAMIC(CInfoSet, CRecordset)
CInfoSet::CInfoSet(CDatabase* pdb)
: CRecordset(pdb)
{
//{{AFX_FIELD_INIT(CInfoSet)
m_id = 0;
m_name = _T("");
m_sex = _T("");
m_score = _T("");
m_nFields = 4;
//}}AFX_FIELD_INIT
m_nDefaultType = snapshot;
}
CString CInfoSet::GetDefaultConnect()
{
return _T("ODBC;DSN=data;UID=sa;PWD=");
}
CString CInfoSet::GetDefaultSQL()
{
return _T("[dbo].[student]");
}
void CInfoSet::DoFieldExchange(CFieldExchange* pFX)
{
//{{AFX_FIELD_MAP(CInfoSet)
pFX->SetFieldType(CFieldExchange::outputColumn);
RFX_Long(pFX, _T("[id]"), m_id);
RFX_Text(pFX, _T("[name]"), m_name);
RFX_Text(pFX, _T("[sex]"), m_sex);
RFX_Text(pFX, _T("[score]"), m_score);
//}}AFX_FIELD_MAP
}
/
// CInfoSet diagnostics
#ifdef _DEBUG
void CInfoSet::AssertValid() const
{
CRecordset::AssertValid();
}
void CInfoSet::Dump(CDumpContext& dc) const
{
CRecordset::Dump(dc);
}
#endif //_DEBUG
#if !defined(AFX_INFOSET_H__247654D2_5E97_4303_AF6F_EB061CF047EC__INCLUDED_)
#define AFX_INFOSET_H__247654D2_5E97_4303_AF6F_EB061CF047EC__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// InfoSet.h : header file
//
/
// CInfoSet recordset
class CInfoSet : public CRecordset
{
public:
CInfoSet(CDatabase* pDatabase = NULL);
DECLARE_DYNAMIC(CInfoSet)
// Field/Param Data
//{{AFX_FIELD(CInfoSet, CRecordset)
long m_id;
CString m_name;
CString m_sex;
CString m_score;
//}}AFX_FIELD
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CInfoSet)
public:
virtual CString GetDefaultConnect(); // Default connection string
virtual CString GetDefaultSQL(); // Default SQL for Recordset
virtual void DoFieldExchange(CFieldExchange* pFX); // RFX support
//}}AFX_VIRTUAL
// Implementation
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_INFOSET_H__247654D2_5E97_4303_AF6F_EB061CF047EC__INCLUDED_)
有示例代码,稍后上传。
标签:CInfoSet,用法,Item,AFX,CGridCtrl,DT,gridCtrl,col 来源: https://blog.csdn.net/canlynetsky/article/details/110306763