Merge Cells in wxGrid Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
fancyivan
Experienced Solver
Experienced Solver
Posts: 80
Joined: Wed May 26, 2010 8:42 am
Location: Beijing, China
Contact:

Merge Cells in wxGrid

Post by fancyivan » Sun Jan 05, 2014 8:24 am

Hello,

Win7 + MinGW32 + wxWidgets3.0.0 + Codebocks13.12

I want to show some data(from database) in wxgrid.
Therefore, I create a class customTable: public wxGridTableBase, and bind it with wxGrid by using SetTable().

And then I need merge every 3 cells of first column:

for example: merge cell(0,0) + (1,0) + (2,0); cell(3,0) + (4,0) + (5,0); ................

Here I can not use wxGrid->SetCellSize(...), because I didn't call CreateGrid(), I use SetTable() instead.
And I checked the sample grid source codes. I found that may be I should create a customer wxGridCellAttrProvider, and do merge cell in GetAttr();
Here it is:

Code: Select all

virtual wxGridCellAttr *GetAttr(int row, int col, wxGridCellAttr::wxAttrKind  kind /* = wxGridCellAttr::Any */) const
        {
            wxGridCellAttr *attr = wxGridCellAttrProvider::GetAttr(row, col, kind);
            wxLogMessage(wxString::Format("GetCellAttr(%d,%d);", row, col));
            //first merge cells for Corner
            if((col < 1) && (n > 1))
            {
                //ensure attr existed
                if(!attr)
                {
                    attr = new wxGridCellAttr();
                    attr->SetBackgroundColour(m_colBg);
                    attr->SetAlignment(wxALIGN_CENTER, wxALIGN_CENTER);
                    attr->SetRenderer(new wxGridCellAutoWrapStringRenderer());
                }
                //set different size for each cell in order to merge cells
                if(row % n)
                {
                    attr->SetSize(-(row % n), 0);
                }
                else
                {
                    attr->SetSize(n, 1);
                }
                return attr;
            }
            // for other common cell
            if(!attr)
            {
                attr = m_attrData;
                attr->IncRef();
            }
            return attr;
        };
I checked the description of wxGrid::GetCellSize() in API document, therefore, I set the size of cell(0,0) to (3, 1), set the size of cell(1,0) to (-1,0), set the size of cell(2,0) to (-2,0).

the defaut data for grid is only 1 cell, If I press the 'ToggleData' button, the data for grid will be changed to 9 rows and 4 cols.
Here I click the cell(0,0) once.
Then If I press the 'ToggleData' button again in order to change the data for grid back to 1 cell, I will get the incorrect CellSize for cell(0,0).
pls check my screenshot or run my app.

pls help, thank you.

Tao
Attachments
TestGrid_bin.rar
(1.14 MiB) Downloaded 58 times
grid_bug.png
grid_bug.png (7.85 KiB) Viewed 1088 times
grid.png
grid.png (16.74 KiB) Viewed 1088 times
OS: Win7 Ultimate SP1 x64(Windows XP Pro SP3 in VirtualBox)
Compiler: MinGW32 (gcc4.8.1 + gdb7.6.1)
IDE: Code::Blocks 12.11
Lib: wxWidgets3.0.0

fancyivan
Experienced Solver
Experienced Solver
Posts: 80
Joined: Wed May 26, 2010 8:42 am
Location: Beijing, China
Contact:

Re: Merge Cells in wxGrid

Post by fancyivan » Mon Jan 13, 2014 2:50 pm

first of all, sorry for my english, may be I didn't describe my question clear.

after debuging and testing again and again at 3 nights, I think I found the right place.

I tested the following codes for cell(0, 0) when I saw incorrect displaying:

Code: Select all

int m, n;
wxGridCellAttrProvider *pro = m_grid->GetTable()->GetAttrProvider();
attr->GetSize(&m, &n);    // correct, m = 1, n = 1
m_grid->GetCellSize(i, j, &m, &n);  // incorrect, m = 3, n = 1
Then I digged into grid.h and grid.cpp, I found that GetCellSize will call GetCellAttr(), then I found that a CachedAttr will be returned here.

Now I can call RefreshAttr(int row, int col) to fix my problem.
May be we should change ClearCache() in wxGrid from protected to public? or shoul call ClearCache() once in SetTable() function? I'm not sure.

Thank you.

Tao
OS: Win7 Ultimate SP1 x64(Windows XP Pro SP3 in VirtualBox)
Compiler: MinGW32 (gcc4.8.1 + gdb7.6.1)
IDE: Code::Blocks 12.11
Lib: wxWidgets3.0.0

Post Reply