# More Adventures with Wx::GridTableBase

**URL:** <https://www.ruby-forum.com/t/more-adventures-with-wx-gridtablebase/225994>\
**Category:** wxRuby\
**Created:** [February 21, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/more-adventures-with-wx-gridtablebase/225994 "2008-02-21T20:05:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![echobinary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@echobinary](https://www.ruby-forum.com/u/echobinary)\
**Post date:** [February 21, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/more-adventures-with-wx-gridtablebase/225994/1 "2008-02-21T20:05:49Z")

</div>

The saga continues!  
I have tried linking a grid to a gridtablebase and gotten mixed results  
but nothing like what i wanted. results have ranged from, nothing to  
crashing the ruby interpreter.

It seems to me you cannot join a GridTableBase to a Grid w/o having data  
in the GridTableBase first.  
Is this true?

Then, I get an error when I didnt have GetAttr defined in my  
GridTableBase.

HALP! 😃

===GridTableBase class ===========  
class TimeTableBase \< Wx::GridTableBase  
def initialize(recordset = nil)  
super()  
@record\_set = recordset  
end

def set\_recordset(recordset)  
@record\_set = recordset  
end

def get\_number\_rows  
return\_value = nil  
if @record\_set != nil  
return\_value = @record\_set.count()  
end  
return return\_value  
end

def get\_number\_cols  
return\_value = nil  
if @record\_set != nil  
return\_value = @record\_set.columns.length  
end  
return return\_value  
end

def get\_col\_label\_value(col)  
return col.to\_s  
end

def is\_empty\_cell(row, col)  
return\_value = true  
cell\_value = get\_value(row, col)  
if (cell\_value != nil)  
if cell\_value != “”  
return\_value = false  
end  
end  
return return\_value  
end

def get\_value(row, col)  
return\_value = nil  
if @record\_set != nil  
begin  
return\_value = @record\_set[@record\_set.size][row].values[col]  
rescue  
end  
end  
return return\_value  
end

def get\_attr(row, col, kind) #  
“” # Not having this in gave this error:  
end #main.rb:26:in `get\_attr’: in method  
# GetAttr’, argument 4 of type  
# wxGridCellAttr::wxAttrKind’  
(TypeError)  
#  
end  
=====Frame class ==================================  
class TimeFrame \< Wx::Frame  
def initialize(timefly,  
title,  
pos,  
size,  
style=  
#Wx::MINIMIZE\_BOX |  
Wx::MAXIMIZE\_BOX |  
Wx::RESIZE\_BORDER |  
Wx::SYSTEM\_MENU |  
Wx::CAPTION |  
Wx::CLOSE\_BOX |  
Wx::CLIP\_CHILDREN)  
super(nil,-1,title,pos,size,style)  
@timefly = timefly  
@database = @timefly.database()  
…  
…  
…  
@time\_grid = Wx::Grid.new(self, -1)  
#@time\_grid\_table = nil  
#@time\_grid\_table = TimeTableBase.new(recordset)  
#@time\_grid.set\_table(@time\_grid\_table) # \<-- this line  
causes # runtime  
failure  
and  
# simply crashes - no  
# error code

```
 evt_button(30) {|evt| on_go_button_pressed(evt)}

```

end

def on\_go\_button\_pressed(evt)  
timelog = @database[:timelog]  
sc\_date =  
@sc\_date.get\_value()#+"/"+@sc\_month.get\_value()+"/"+@sc\_day.get\_value()  
recordset = timelog#.filter(:time\_open =\> sd\_date)

```
 if recordset != nil
   @time_grid_table = nil
   @time_grid_table = TimeTableBase.new(recordset)
   #@time_grid.set_table(@time_grid_table) # <-- this line

```

# causes # runtime failure and # simply crashes - no # error code end end end

---

<div class="post-metadata">

**Author:** ![echobinary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@echobinary](https://www.ruby-forum.com/u/echobinary)\
**Post date:** [February 21, 2008, 10:10pm UTC](https://www.ruby-forum.com/t/more-adventures-with-wx-gridtablebase/225994/2 "2008-02-21T22:10:46Z")

</div>

EchoB wrote:

> I have tried linking a grid to a gridtablebase and gotten mixed results  
> but nothing like what i wanted. results have ranged from, nothing to  
> crashing the ruby interpreter.

Please do report crashes, ideally using the bug tracker. Whatever the  
problem, wxRuby should raise an exception rather than segfault. But we  
need a complete, self-contained, minimal sample that demonstrates the  
problem.

> It seems to me you cannot join a GridTableBase to a Grid w/o having data  
> in the GridTableBase first.  
> Is this true?

Well, whatever your GridTableBase class is, it is obliged to return  
meaningful values for the required methods (eg get\_number\_cols,  
get\_number\_rows) whatever its internal state. From the code you posted,  
it seems if the “recordset” is not defined in your GridTable class, it  
returns nil for those methods. Which it shouldn’t do - at least return  
0.

As I’ve suggested before, I’m not sure why you’re trying to use  
GridTableBase rather than just using a straightforward Wx::Grid and  
calling set\_value to populate the table whenever the recordset is  
changed. It would only really be sensible if your recordset is so huge  
that filling the table manually is too slow.

alex

---

<div class="post-metadata">

**Author:** ![echobinary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/90db22/32.png) [@echobinary](https://www.ruby-forum.com/u/echobinary)\
**Post date:** [February 28, 2008, 11:03am UTC](https://www.ruby-forum.com/t/more-adventures-with-wx-gridtablebase/225994/3 "2008-02-28T11:03:28Z")

</div>

> Well, whatever your GridTableBase class is, it is obliged to return  
> meaningful values for the required methods (eg get\_number\_cols,  
> get\_number\_rows) whatever its internal state. From the code you  
> posted,  
> it seems if the “recordset” is not defined in your GridTable class,  
> it  
> returns nil for those methods. Which it shouldn’t do - at least  
> return 0.
> 
> As I’ve suggested before, I’m not sure why you’re trying to use  
> GridTableBase rather than just using a straightforward Wx::Grid and  
> calling set\_value to populate the table whenever the recordset is  
> changed. It would only really be sensible if your recordset is so  
> huge  
> that filling the table manually is too slow.

Ok. What is the order of operations one should use to define a grid, and  
then a gridtablebase?  
My dataset is going to change many times at runtime.  
Here is the larger picture:  
My application is a time tracking application, working like a punch  
clock. So projectIDs, TimeIn, TimeOut,  
and descriptions can get into the hundreds per week and defiantly per  
month.  
I have an Sqlite3 database with Sequel as the abstraction layer  
retrieving the dataset.  
I have completed the “data entry” portion of my project. In order for  
that information to be useful,  
I need to be able to view it, etc. I am passing Sequel datasets to my  
derived wxGridTableBase class.  
The nature of my dataset is that I would like to adjust the dataset on  
an event driven basis.  
For example button(s) to switch views between:  
week summary of hours  
month summary of hours  
daily summary of hours  
etc..

All ordered by projectID. I can handle the sql queries. But I have yet  
to even be able to successfully link the wxGrid to the wxGridTableBase.  
I have scoured forums and the wxRuby docs, sample code (BigDemo,  
etc…), and asked in #wxruby. Google returns results in wxPython, but  
that doesn’t help me.  
I guess I just don’t know what I’m doing with it yet.

To sum up:

1. What is the best practice or order of operations for defining a  
wxGrid and then linking it to a wxGridTableBase?
2. Is the linking in #1 possible to do on a runtime event-driven basis?
3. Is there any wxRuby demo code anywhere? Or where can I find more  
ruby/wxruby 'centric documentation on this class?
