# Ask gridtablebase

**URL:** <https://www.ruby-forum.com/t/ask-gridtablebase/195705>\
**Category:** wxRuby\
**Created:** [September 28, 2010, 2:59pm UTC](https://www.ruby-forum.com/t/ask-gridtablebase/195705 "2010-09-28T14:59:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [September 28, 2010, 2:59pm UTC](https://www.ruby-forum.com/t/ask-gridtablebase/195705/1 "2010-09-28T14:59:18Z")

</div>

Dear all, I need help

I intent to make a gridtablebase class to save my data temporary and  
display it on a grid  
Due to my assumption, if I create a gridtablebase object and connect  
it to a grid object, the grid should display what’s in gridtablebase  
object

here is part of the code,

class Gridtablebase\_ds \< Wx::GridTableBase  
def initialize()  
super  
@data = []  
@col\_names = [‘Col1’, ‘Col2’]  
@data \<\< [‘1’, ‘John’]  
@data \<\< [‘1’, ‘Doe’]  
end

def get\_value(row, col)  
(@data[row])[col]  
end

def append\_data(data=[])  
@data \<\< data  
end

#some other code here  
end

in my other class, I use Gridtablebase\_ds as follow

@ds = Detil\_Gridtablebase\_ds.new  
@grid.set\_table @ds  
@grid.refresh

until now it works fine, but then I tried to add data to  
Gridtablebase\_ds object

@ds.append\_data [‘3’, ‘McGyver’]

but the grid doesn’t change, and I can’t find any clue to where the  
mistakes is  
What is wrong here, perhaps I miss something

Thank you  
Regards  
Hendra

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [September 28, 2010, 3:29pm UTC](https://www.ruby-forum.com/t/ask-gridtablebase/195705/2 "2010-09-28T15:29:45Z")

</div>

hi hendra

On 28/09/2010 13:58, hendra kusuma wrote:

> Due to my assumption, if I create a gridtablebase object and connect  
> it to a grid object, the grid should display what’s in gridtablebase  
> object

yes, but …

> until now it works fine, but then I tried to add data to Gridtablebase\_ds object
> 
> @ds.append\_data [‘3’, ‘McGyver’]
> 
> but the grid doesn’t change

… there is no automatic trigger to update the visual display when the  
underlying table changes. call @grid.refresh if you need to.

best  
alex

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [September 29, 2010, 5:54pm UTC](https://www.ruby-forum.com/t/ask-gridtablebase/195705/3 "2010-09-29T17:54:29Z")

</div>

On Tue, Sep 28, 2010 at 9:09 PM, Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> yes, but …  
> … there is no automatic trigger to update the visual display when the  
> underlying table changes. call @grid.refresh if you need to.

Thanks Alex  
I already done that and it is not working  
well, I found the answer from archive email anyway  
back then somebody asked the same thing and the conclusion was  
wxgrid seems to copy the gridtablebase object.  
I guess there is no direct connection between gridtablebase object and  
the  
one wxgrid copy  
and due to my experiment,  
I can’t even modify the object with get\_table method  
something like wxgrid.get\_table.add\_data([‘3’, ‘McGyver’]) also does not  
work

I think it was you who said that it is the behaviour of wxwidget, so it  
will  
stay like that.  
But perhaps I am wrong

I end up making a datasource object to handle the situation and  
refresh the grid my remaking the gridtablebase object and re-set the  
grid  
table

Thanks

---

<div class="post-metadata">

**Author:** ![hendra\_kusuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5f8ce5/32.png) [@hendra\_kusuma](https://www.ruby-forum.com/u/hendra_kusuma)\
**Post date:** [October 8, 2010, 10:33am UTC](https://www.ruby-forum.com/t/ask-gridtablebase/195705/4 "2010-10-08T10:33:14Z")

</div>

On Wed, Sep 29, 2010 at 11:41 PM, hendra kusuma  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > > it to a grid object, the grid should display what’s in gridtablebase
> 
> back then somebody asked the same thing and the conclusion was  
> But perhaps I am wrong
> 
> I end up making a datasource object to handle the situation and  
> refresh the grid my remaking the gridtablebase object and re-set the grid  
> table
> 
> Thanks

I was wrong. You’re right Alex,

it is possible to update grid with @grid.refresh  
I rewrite my gridtablebase object and now it can handle @grid.refresh

here is my code

require ‘rubygems’  
require ‘wx’

# class Dataset\_h \< Wx::GridTableBase def initialize(par={}) super() if par[:colnames] then @colnames = par[:colnames] else @colnames = end if par[:keys] then @keys = par[:keys] else @keys

end  
if par[:rs] then set\_rs(par[:rs]) else @rs =  
 end

```
    @display_data = @rs
    @rows = @display_data.size
    @cols = @keys.size
end

def get_number_rows
    @rows
rescue
    0
end

def get_number_cols
    @cols
rescue
    0
end
def get_value(row, col)
    ((@display_data[row])[@keys[col].to_sym]).to_s
rescue
    ""
end

def get_attr(row, col, attr_kind)
    Wx::GridCellAttr.new
end

def is_empty_cell(row, col)
    true
end

def get_col_label_value(col)
    @colnames[col]
rescue
    ""
end

def set_keys(keys=[])
    @keys = keys
end

def set_rs(rs)
    @rs = []
    rs.each do |key, value|
        @rs << value
    end
end

def auto_rs(rs)
    set_rs(rs)
    @display_data = @rs
    @keys = rs.keys

    @colnames = []
    @keys.each do |key|
        @colnames << key.gsub('_', ' ').capitalize
    end
end

def filter(par)
    tmp = []
    @rs.each do |row|
        par.each do |key, value|
            if row[key.to_sym] == value
                tmp << row
            end
        end
    end

    @display_data = tmp.uniq
    @rows = @display_data.size
end

def filter_like(par)
    tmp = []
    @rs.each do |row|
        par.each do |key, value|
            if row[key.to_sym].to_s.index(value)
                tmp << row
            end
        end
    end

    @display_data = tmp.uniq
    @rows = @display_data.size
end

def add(data)
    @rs << data
end

def delete(row)
    @rs[row] = nil
    @rs.compact
end

def all
    @display_data = @rs
end

def first
    @display_data.first
end

def last
    @display_data.last
end

```

end

but then I found another problem  
I want this object to be a memory storage, where I can add, update, or  
delete and filter rows.

but when I modify data in gridtablebase object, grid is updated, but not  
quite perfectly  
the grid does not update its row number (and perhaps col number, I have  
not  
tried that)  
so when I filtered, there is empty row below last row

even worse when I add data, if gridtablebase data row counts more than  
grid  
row  
grid will not append rows

is there a way to handle this? since this is so close to what I need,  
I really hope there is a way to handle this problem

Thank you
