# Grid vs GridTableBase| + Sequel + SQLite3

**URL:** <https://www.ruby-forum.com/t/grid-vs-gridtablebase-sequel-sqlite3/128003>\
**Category:** wxRuby\
**Created:** [February 4, 2008, 5:14am UTC](https://www.ruby-forum.com/t/grid-vs-gridtablebase-sequel-sqlite3/128003 "2008-02-04T05:14:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EchoB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@EchoB](https://www.ruby-forum.com/u/EchoB)\
**Post date:** [February 4, 2008, 5:14am UTC](https://www.ruby-forum.com/t/grid-vs-gridtablebase-sequel-sqlite3/128003/1 "2008-02-04T05:14:26Z")

</div>

Hey all,  
Wondering if i could get an example of working code that ties Sequel (or  
any other database layer) into a GridTableBase and then into a Grid.  
I am just now trying it and i cant seem to get it to work - no errors  
(well - sometimes) but it is also not displaying anything.  
Code follows:  
class TimeFrame \< Wx::Frame  
def initialize()  
…  
…  
@time\_grid = Wx::Grid.new(self, -1)  
@time\_grid\_table = TimeTableBase.new()\*\*  
…  
end

def on\_go\_button\_pressed(evt)  
…  
… # define a recordset that can contain 0 or more rows (0 rows  
will not make it nil)  
if recordset != nil  
column\_no = 1  
p recordset.columns  
@time\_grid\_table.append\_cols(recordset.columns.length)  
recordset.columns.each {|column|  
@time\_grid\_table.set\_col\_label\_value(column\_no, column.to\_s)  
column\_no = column\_no +1  
}  
@time\_grid\_table.insert\_rows(2)  
@time\_grid.set\_table(@time\_grid\_table)  
end  
end  
##################  
\*\*  
class TimeTableBase \< Wx::GridTableBase  
def initialize  
super()  
end  
def get\_attr row, col, attr\_kind  
##  
end

def get\_number\_rows  
super.get\_number\_rows()  
end

def get\_number\_cols  
super.get\_number\_cols()  
end

def get\_col\_label\_value col  
super.get\_col\_label\_value(col)  
end

def is\_empty\_cell row, col  
super.is\_empty\_cell(row, col)  
end

def get\_value row, col  
super.get\_value(row, col)  
end  
end

---

<div class="post-metadata">

**Author:** ![EchoB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@EchoB](https://www.ruby-forum.com/u/EchoB)\
**Post date:** [February 5, 2008, 1:53pm UTC](https://www.ruby-forum.com/t/grid-vs-gridtablebase-sequel-sqlite3/128003/2 "2008-02-05T13:53:26Z")

</div>

EchoB wrote:

> Hey all,  
> Wondering if i could get an example of working code that ties Sequel (or  
> any other database layer) into a GridTableBase and then into a Grid.

It depends what your’e trying to do with the database. I couldn’t work  
out what your sample code was meant to do.

There’s nothing that says if you’re working with a SQL db, you must use  
GridTableBase. If you simply want to display results from a SQL query as  
rows in a Grid, it might well be easier to populate the Grid with  
create\_table and set\_cell\_value, and not mess around with GridTable.

GridTable is probably useful if:

1. You have a huge dataset, and want to avoid loading it all at once.  
GridTable will only load cell values as they are visible.
2. There is a tight connection between a cell’s value and a stored  
value; then you can overload set\_value in your GridTable class and write  
any changes straight back to the database.

> I am just now trying it and i cant seem to get it to work - no errors  
> (well - sometimes) but it is also not displaying anything.  
> A couple of things stick out:

- you should call Grid#set\_table, once, immediately after initialising  
the grid, not in an event handler.
- you must return valid values (not call super) for the methods listed  
in the GridTableBase documentation (get\_number\_cols, is\_empty\_cell etc)

alex

---

<div class="post-metadata">

**Author:** ![EchoB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@EchoB](https://www.ruby-forum.com/u/EchoB)\
**Post date:** [February 6, 2008, 3:33pm UTC](https://www.ruby-forum.com/t/grid-vs-gridtablebase-sequel-sqlite3/128003/3 "2008-02-06T15:33:23Z")

</div>

Alex F. wrote:

> It depends what your’e trying to do with the database. I couldn’t work  
> out what your sample code was meant to do.  
> I get that a lot 😃

> There’s nothing that says if you’re working with a SQL db, you must use  
> GridTableBase. If you simply want to display results from a SQL query as  
> rows in a Grid, it might well be easier to populate the Grid with  
> create\_table and set\_cell\_value, and not mess around with GridTable.  
> I thought about it, and thats what I was initially trying to do, but  
> GridTable _sounds_ nicer. I want to do things like sorting, and  
> grouping, and other stuff on the fly.

Are there any working examples of how to use GridTableBase?  
From [http://wxruby.rubyforge.org/doc/gridtablebase.html](http://wxruby.rubyforge.org/doc/gridtablebase.html) I get that in  
order to use the class I have to at least override:

- get\_attr-  
“May be overridden to handle attributes directly in the table.”

- get\_number\_cols-  
“Should return the total number of columns in the grid. You must  
override this functions in a derived table class.”

- get\_number\_rows-  
“Should return the total number of rows in the grid. You must override  
these functions in a derived table class.”

- get\_value-  
“Should return the content of the specified cell as a Ruby string. You  
must override this function in a derived table class.”

- is\_empty\_cell-  
“Should return true if a cell is empty. You must override this  
function in a derived table class.”

To me this seems to say that the gridtablebase is depending on the grid  
(which is depending on the gridtablebase for information) for  
information. (That made my head hurt)

Perhaps an updated version of bigdemo.rb? 😃  
Anyways, thanks for bearing with me!  
:EchoBinary
