无聊做了一个小示例——在 CBCGPGridCtrl 网格控件中增加行、删除行以及上下移动行。代码放下面了:
BOOL CtestDlg::OnInitDialog()
{
CBCGPDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CRect rect(0, 0, 400, 400);
m_pGridCtrl = new CBCGPGridCtrl;
m_pGridCtrl->Create(WS_CHILD, rect, this, 1);
m_pGridCtrl->ShowWindow(SW_SHOW);
m_pGridCtrl->InsertColumn(0, _T("name"), 40);
m_pGridCtrl->InsertColumn(1, _T("sex"), 40);
CBCGPGridRow* pRow = new CBCGPGridRow;
m_pGridCtrl->AddRow(pRow);
CBCGPGridItem* pNameItem = pRow->CreateItem(0, 0);
pRow->AddItem(pNameItem);
pNameItem->SetValue("Nancy");
CBCGPGridItem* pSexItem = pRow->CreateItem(0, 1);
pRow->AddItem(pSexItem);
pSexItem->AddOption("男");
pSexItem->AddOption("女");
pSexItem->SetValue("男");//默认值为“男”
return TRUE; // return TRUE unless you set the focus to a control
void CtestDlg::OnBnClickedBtnAdd()
{
CBCGPGridRow* pRow = new CBCGPGridRow;
m_pGridCtrl->AddRow(pRow);
int iRow = pRow->GetRowId();
CBCGPGridItem* pNameItem = pRow->CreateItem(iRow, 0);
pRow->AddItem(pNameItem);
CBCGPGridItem* pSexItem = pRow->CreateItem(iRow, 1);
pRow->AddItem(pSexItem);
pSexItem->AddOption("男");
pSexItem->AddOption("女");
pSexItem->SetValue("男");
}
void CtestDlg::OnBnClickedBtnDelete()
{
CBCGPGridRow* pRow = m_pGridCtrl->GetCurSel();
if (!pRow)
{
return;
}
int iRow = pRow->GetRowId();
m_pGridCtrl->RemoveRow(iRow);
CBCGPGridRow* pPreRow = m_pGridCtrl->GetRow(iRow - 1);
m_pGridCtrl->AdjustLayout();
m_pGridCtrl->SetCurSel(pPreRow);
}
void CtestDlg::OnBnClickedBtnMoveUp()
{
CBCGPGridRow* pRow = m_pGridCtrl->GetCurSel();
if (!pRow)
{
return;
}
int iRow = pRow->GetRowId();
CBCGPGridRow* pNewRow = new CBCGPGridRow;
m_pGridCtrl->InsertRowBefore(iRow - 1, pNewRow);
CBCGPGridItem* pNameItem = pNewRow->CreateItem(0, 0);
pNewRow->AddItem(pNameItem);
pNameItem->SetValue(pRow->GetItem(0)->GetValue());
CBCGPGridItem* pSexItem = pNewRow->CreateItem(0, 1);
pNewRow->AddItem(pSexItem);
pSexItem->AddOption("男");
pSexItem->AddOption("女");
pSexItem->SetValue(pRow->GetItem(1)->GetValue());
m_pGridCtrl->RemoveRow(iRow + 1);
m_pGridCtrl->AdjustLayout();
m_pGridCtrl->SetCurSel(iRow - 1);
}
void CtestDlg::OnBnClickedBtnMoveDown()
{
CBCGPGridRow* pRow = m_pGridCtrl->GetCurSel();
if (!pRow)
{
return;
}
int iRow = pRow->GetRowId();
CBCGPGridRow* pNewRow = new CBCGPGridRow;
m_pGridCtrl->InsertRowAfter(iRow+1, pNewRow);
CBCGPGridItem* pNameItem = pNewRow->CreateItem(0, 0);
pNewRow->AddItem(pNameItem);
pNameItem->SetValue(pRow->GetItem(0)->GetValue());
CBCGPGridItem* pSexItem = pNewRow->CreateItem(0, 1);
pNewRow->AddItem(pSexItem);
pSexItem->AddOption("男");
pSexItem->AddOption("女");
pSexItem->SetValue(pRow->GetItem(1)->GetValue());
m_pGridCtrl->RemoveRow(iRow);
m_pGridCtrl->AdjustLayout();
m_pGridCtrl->SetCurSel(iRow+1);
}