# Creating a ... table/matrix/grid?

**URL:** <https://www.ruby-forum.com/t/creating-a-table-matrix-grid/164775>\
**Category:** wxRuby\
**Created:** [April 5, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/creating-a-table-matrix-grid/164775 "2009-04-05T20:13:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [April 5, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/creating-a-table-matrix-grid/164775/1 "2009-04-05T20:13:02Z")

</div>

Hey guys. First post, and I have a project which is ambitious (to me  
anyway, since this is my first foray ever in GUI).  
I want to dynamically create (from 2 arrays for instance) a table with  
rows and columns, and each ‘cell’ would behave like a checkbox, sending  
a query to a database tailored after the row/column in which it is (a  
very simple query).

I’ve successfully done the same purpose with nested loops in a text-only  
Ruby environment. The same purpose could be obtained from, say, two  
drop-down menus selecting the options and pressing a button - but I want  
a quick, graphical way of doing it all (maybe check all the boxes then  
use a submit button, that’d work too)…

Now that I’ve explained all that… I am a complete newbie at wxruby. I  
followed the first three or so tutorials but I don’t really know where  
to go from there. Does anyone have a theoretical explanation of how to  
accomplish what I want to do… And maybe intermediary projects I can  
accomplish to get there?

Sorry if I’m rambling a little… Thanks in advance 🙂  
–Aldric

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [April 5, 2009, 11:11pm UTC](https://www.ruby-forum.com/t/creating-a-table-matrix-grid/164775/2 "2009-04-05T23:11:02Z")

</div>

Hi Aldric

Aldric G. wrote:

> Hey guys. First post, and I have a project which is ambitious (to me  
> anyway, since this is my first foray ever in GUI).  
> I want to dynamically create (from 2 arrays for instance) a table with  
> rows and columns, and each ‘cell’ would behave like a checkbox, sending  
> a query to a database tailored after the row/column in which it is (a  
> very simple query).

Welcome to wxRuby which, happily, has a widget that I think will be very  
well suited to your project. It’s called Wx::Grid, and the documentation  
is here:

[http://wxruby.rubyforge.org/doc/grid.html](http://wxruby.rubyforge.org/doc/grid.html)

Note that there is one missing section we recently noticed in this doc,  
which describes some of the events generated by grid. Have a look also  
at:

[http://docs.wxwidgets.org/stable/wx\_wxgrid.html](http://docs.wxwidgets.org/stable/wx_wxgrid.html)

There are two quite comprehensive grid samples that came with your  
wxruby installation, in /samples/grid/. You could study these as  
an intermediary project.

Briefly, on some of the things you want to do:

- to display cells with check-box like appearance, use  
Wx::GridCellBoolRenderer and Wx::GridCellBoolEditor classes. If you find  
you don’t like the default way these work, you can also completely  
customise how cells are displayed using your own subclass of  
Wx::GridCellRenderer.

- to handle an action when a cell is checked or unchecked, set up an  
event handler on evt\_grid\_cell\_change.

- if you already have the code that interacts with the database, an  
efficient way to link this to the GUI display can be to use  
GridTableBase. I won’t go into detail, but have a look at the sample -  
if you’re used to model/view separation this works nicely.

hth  
alex
