Search code examples
wxwidgets

wxWidgets - wxGrid - reading/writing non string cell values


I have a wxGrid to edit an array of numerical data.

I was wondering what's the best way to get non-string data in and out of the cells without going through the string to numeric conversion all the time. I've used SetCellEditor() to control the data entry.

currently I use this:

// numeric value into cell
str.clear();
str << val1;
m_grid4->SetCellValue(row, col, str);
..
// read value from back into variable
val = atoi(m_grid4->GetCellValue(row, col));

Apart from the fact that atoi() is a bit ugly and a template function with a stringstream would be better, is there a way do get non-string values a bit better in and out of cells?

I was looking at the editors and renderers but can't figure it out.


Solution

  • If you worry about efficiency, you almost certainly should use a custom table class deriving from wxGridTableBase instead of using the default trivial wxGridStringTable implementation which stores everything as strings. Then, and much less importantly, if it makes sense in your case, you can use wxGridCellNumberRenderer which will call your table GetValueAsLong() method instead of GetValue() (which returns a string).

    Both of those are demonstrated in wxGrid sample, notably look at BugsGridTable there.

    Good luck!