GridCtrl学习笔记(3)一行一行地更新表格,有bug版

代码:
运行结果:
在鼠标不去点、使用滚动条的时候,程序运行正常,一行一行地更新;
Bug说明:
1、更新的时候,如果格子有一小块已经显示出来,滚动条不会动;
2、鼠标点击滚动条后,程序运行会有问题
提示错误的地方:
  1. int CGridCtrl::GetRowHeight(int nRow) const
  2. {
  3. ASSERT(nRow >= 0 && nRow < m_nRows);
  4. if (nRow < 0 || nRow >= m_nRows)
  5. return -1;
  6. return m_arRowHeights[nRow];
  7. }
我的代码如下:
  1. void CGridControlTest03Dlg::GridCtrlInit()
  2. {
  3. m_pGrid.SetEditable(true);
  4. m_pGrid.SetTextBkColor(RGB(0xFF, 0xFF, 0xE0));//黄色背景
  5. m_pGrid.SetRowCount(16); //初始为100行
  6. m_pGrid.SetColumnCount(26); //初始化为25列
  7. m_pGrid.SetFixedRowCount(1); //表头为一行
  8. m_pGrid.SetFixedColumnCount(1); //表头为一列
  9. for (int row = 0; row < m_pGrid.GetRowCount(); row++)
  10. for (int col = 0; col < m_pGrid.GetColumnCount(); col++)
  11. {
  12. //设置表格显示属性
  13. GV_ITEM Item;
  14. Item.mask = GVIF_TEXT|GVIF_FORMAT;
  15. Item.row = row;
  16. Item.col = col;
  17. m_pGrid.SetRowHeight(row,25); //设置各行高
  18. m_pGrid.SetColumnWidth(0,64); //设置0列宽
  19. m_pGrid.SetColumnWidth(col,64); //设置各列宽
  20. if(row==0&&col==0) //第(0,0)格
  21. {
  22. Item.nFormat = DT_CENTER|DT_WORDBREAK;
  23. Item.strText.Format(_T("报表显示"),col);
  24. }
  25. else if (row < 1) //设置0行表头显示
  26. {
  27. Item.nFormat = DT_CENTER|DT_WORDBREAK;
  28. Item.strText.Format(_T(" 项目%d"),col);
  29. }
  30. else if (col < 1) //设置0列表头显示
  31. {
  32. if(row< m_pGrid.GetRowCount())
  33. {
  34. Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
  35. Item.strText.Format(_T("第%d次"),row);
  36. }
  37. }
  38. else
  39. {
  40. Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
  41. Item.strText.Format(_T(""),2);
  42. }
  43. m_pGrid.SetItem(&Item);
  44. }
  45. }

  1. void CGridControlTest03Dlg::OnBnClickedAutofillbox()
  2. {
  3. // TODO: Add your control notification handler code here
  4. int col;
  5. GV_ITEM Item;
  6. rowCnt++;
  7. if (rowCnt > m_pGrid.GetRowCount())
  8. {
  9. rowCnt = 1;
  10. TextTemp = (~TextTemp)&0x00ff;
  11. }
  12. Item.mask = GVIF_TEXT|GVIF_FORMAT;
  13. Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
  14. for (col = 1; col < m_pGrid.GetColumnCount(); col++)
  15. {
  16. Item.row = rowCnt;
  17. Item.col = col;
  18. //Item.strText.Format(_T("55"),rowCnt);
  19. Item.strText.Format(_T("%x"),TextTemp,rowCnt);
  20. m_pGrid.SetItem(&Item);
  21. }
  22. m_pGrid.Refresh();
  23. if (!m_pGrid.IsCellVisible(rowCnt, 1))
  24. {
  25. int scrollPos = m_pGrid.GetScrollPos32(SB_VERT);
  26. CCellID idTopLeft = m_pGrid.GetTopleftNonFixedCell();
  27. int yScroll = m_pGrid.GetRowHeight(idTopLeft.row);
  28. if (rowCnt == 1 && scrollPos > 0)
  29. {
  30. scrollPos = 0;
  31. yScroll = 0;
  32. }
  33. m_pGrid.SetScrollPos32(SB_VERT, scrollPos + yScroll);
  34. }
  35. }

原文地址:https://www.cnblogs.com/ciuciu/p/4462788.html