网格控件群组的高级使用
网格控件群组的高级使用
本例要解决的问题是:成绩按优良中差分组。
一,新建一个基于对话框的BCG工程,单ChengJiGridCtrl。
二,新增CChengJiGridCtrl类,派生于网格控件。头文件如下:
class CChengJiGridCtrl : public CBCGPGridCtrl {public:CChengJiGridCtrl();virtual ~CChengJiGridCtrl();virtual int CompareGroup (const CBCGPGridRow* pRow1, const CBCGPGridRow* pRow2, int iColumn);virtual CString GetGroupName (int nGroupCol, CBCGPGridItem* pItem);//{{AFX_MSG(CColorGridCtrl)afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);//}}AFX_MSGDECLARE_MESSAGE_MAP()};源文件如下:BEGIN_MESSAGE_MAP(CChengJiGridCtrl, CBCGPGridCtrl)//{{AFX_MSG_MAP(CColorGridCtrl)ON_WM_CREATE()//}}AFX_MSG_MAP END_MESSAGE_MAP()CChengJiGridCtrl::CChengJiGridCtrl(){}CChengJiGridCtrl::~CChengJiGridCtrl(){}int CChengJiGridCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) {if (CBCGPGridCtrl::OnCreate(lpCreateStruct) == -1)return -1;EnableMarkSortedColumn (TRUE, FALSE);EnableHeader (TRUE, BCGP_GRID_HEADER_MOVE_ITEMS | BCGP_GRID_HEADER_SORT);EnableRowHeader (TRUE);EnableLineNumbers ();EnableInvertSelOnCtrl ();const int nColumns = 2; InsertColumn (0, "姓名", 80);InsertColumn (1, "成绩", 80);//-----------------------// 以第二列分组//-----------------------InsertGroupColumn (0, 1);_variant_t var[][nColumns] = {{_T("张三"),(long)92},{_T("李四"),(long)55},{_T("王五"),(long)70},{_T("赵六"),(long)95},};const int iRowCnt = sizeof(var)/sizeof(var[0]);for (int i = 0; i < iRowCnt ; i++){CBCGPGridRow* pRow = CreateRow (GetColumnCount ());for(int j = 0 ; j < nColumns ; j++)pRow->GetItem (j)->SetValue (var[i][j]);AddRow (pRow, FALSE);}SetReadOnly ();AdjustLayout ();return 0;}int GetLeve(long l){if( l >= 90 )return 4;if( l >= 80 )return 3;if( l >= 70 )return 2;if( l >= 60 )return 1;return 0;}//********************************************************************************int CChengJiGridCtrl::CompareGroup (const CBCGPGridRow* pRow1, const CBCGPGridRow* pRow2, int /*iColumn*/){ASSERT_VALID (this);ASSERT_VALID (pRow1);ASSERT_VALID (pRow2);long lVal1 = (long) pRow1->GetItem (1)->GetValue ();long lVal2 = (long) pRow2->GetItem (1)->GetValue ();return GetLeve(lVal2) - GetLeve(lVal1);}//********************************************************************************CString CChengJiGridCtrl::GetGroupName (int /*iColumn*/, CBCGPGridItem* pItem){ASSERT_VALID (this);ASSERT_VALID (pItem);long lVal = (long) pItem->GetValue ();long lLeve = GetLeve(lVal);if( 4 == lLeve )return _T("优");else if( 3 == lLeve) return _T("良");else if( 2 == lLeve )return _T("中");else if( 1 == lLeve )return _T("合格");return _T("不合格");}三,CChengJiGridCtrlDlg类增加变量CChengJiGridCtrl m_grid;四,CChengJiGridCtrlDlg::OnInitDialogCRect r(0,0,300,300);m_grid.Create(WS_CHILD, r, this, 1); m_grid.ShowWindow(SW_SHOW);
五,Ctrl+F5查看运行效果。如图:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~