Search code examples
wxwidgets

wxGrid shows large empty border on right


By default, wxGrid shows a small ( 10 pixels? ) blank border on the right hand side, after the last column. Calling SetMargins() has no effect on it.

It is irritating, but I can live with it.

However, if I set the the row label width to zero then the blank border grows much larger. If I have just one column, the effect is horrible. It looks like wxGrid is leaving room for the non-existent label.

myPatGrid = new wxGrid(panel,IDC_PatGrid,wxPoint(10,10),wxSize(150,300) );
myPatGrid->SetRowLabelSize(0); 
myPatGrid->CreateGrid(200,1);
myPatGrid->SetColLabelValue(0,L"Patient IDs");

Is there a way to remove this border?

enter image description here

Note that if I set the size of the wxgrid window to narrower in the wxGrid constructor, hoping to hide the border, I now get a horizontal scroll bar which is horrible too.

myPatGrid = new wxGrid(panel,IDC_PatGrid,wxPoint(10,10),wxSize(100,300) );
myPatGrid->SetRowLabelSize(0); 
myPatGrid->CreateGrid(200,1);
myPatGrid->SetColLabelValue(0,L"Patient IDs");

Gives me

enter image description here

I just upgraded to wxWidgets v2.8.12 - problem still exists.


Solution

  • I didn't find an "autosize" function to fit columns in the grid space. As a workaround, if you have only one column set its width to

    myPatGrid->SetColMinimalWidth(0, grid_width - wxSYS_VSCROLL_X - 10)
    

    otherwise, sum other column's width and adapt the last one to fit the remaining space (minus scrollbar width, minus 10).

    EDIT: I have a working example, which produces this:

    grid example

    int gridSize = 150;
    int minSize = gridSize - wxSYS_VSCROLL_X - 2; // scrollbar appear if higher
    grid->SetRowLabelSize(0);
    grid->SetColMinimalWidth(0, minSize);
    grid->SetColSize(0, minSize); // needed, otherwise column will not resize
    grid->ForceRefresh();
    grid->SetColLabelValue(0, "COORD");
    

    EDIT2: I succeded to remove the remaining margin with this:

    int gridSize = 150;
    int minSize = gridSize - 16; // trial & error
    grid->SetMargins(0 - wxSYS_VSCROLL_X, 0);
    

    enter image description here