表格中的某一列的值为0 、1、2。如果想显示为 等待、生产、完成等字符串,使用官方的wxGrideCellEnumRenderer有点小bug,需要自定义一个render来实现。
class MyGridCellEnumRenderer : public wxGridCellChoiceRenderer
{
public:
MyGridCellEnumRenderer( const wxString& choices = wxEmptyString );
// draw the string right aligned
virtual void Draw(wxGrid& grid,
wxGridCellAttr& attr,
wxDC& dc,
const wxRect& rect,
int row, int col,
bool isSelected) wxOVERRIDE;
virtual wxSize GetBestSize(wxGrid& grid,
wxGridCellAttr& attr,
wxDC& dc,
int row, int col) wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE;
protected:
wxString GetString(const wxGrid& grid, int row, int col);
};
MyGridCellEnumRenderer::MyGridCellEnumRenderer(const wxString& choices)
{
if (!choices.empty())
SetParameters(choices);
}
wxGridCellRenderer *MyGridCellEnumRenderer::Clone() const
{
MyGridCellEnumRenderer *renderer = new MyGridCellEnumRenderer;
renderer->m_choices = m_choices;
return renderer;
}
wxString MyGridCellEnumRenderer::GetString(const wxGrid& grid, int row, int col)
{
wxGridTableBase *table = grid.GetTable();
wxString text;
long choiceno;
text = table->GetValue(row, col).Trim();
if(text.IsEmpty() | text.IsNull() | !text.IsNumber()){
text="";
}else{
if(text.Length()==0){
text ="";
}else{
choiceno = wxAtol(text.Trim());
text.Printf(wxT("%s"), m_choices[ choiceno ].c_str() );
}
}
//If we faild to parse string just show what we where given?
return text;
}
void MyGridCellEnumRenderer::Draw(wxGrid& grid,
wxGridCellAttr& attr,
wxDC& dc,
const wxRect& rectCell,
int row, int col,
bool isSelected)
{
wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected);
SetTextColoursAndFont(grid, attr, dc, isSelected);
wxRect rect = rectCell;
rect.Inflate(-1);
// draw the text right aligned by default
grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, attr,
wxALIGN_RIGHT);
}
wxSize MyGridCellEnumRenderer::GetBestSize(wxGrid& grid,
wxGridCellAttr& attr,
wxDC& dc,
int row, int col)
{
return DoGetBestSize(attr, dc, GetString(grid, row, col));
}