# Column as boolean with wxgridtablebase

**URL:** <https://www.ruby-forum.com/t/column-as-boolean-with-wxgridtablebase/185595>\
**Category:** wxRuby\
**Created:** [March 23, 2010, 7:24am UTC](https://www.ruby-forum.com/t/column-as-boolean-with-wxgridtablebase/185595 "2010-03-23T07:24:21Z")\
**Posts on this page:** 5\
**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:** [March 23, 2010, 7:24am UTC](https://www.ruby-forum.com/t/column-as-boolean-with-wxgridtablebase/185595/1 "2010-03-23T07:24:21Z")

</div>

Dear all,

How do I set first column as boolean (shown as checkbox)  
if I prepare my data with wxgridtablebase?  
grid.set\_col\_format\_bool does not work

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:** [March 23, 2010, 1:32pm UTC](https://www.ruby-forum.com/t/column-as-boolean-with-wxgridtablebase/185595/2 "2010-03-23T13:32:32Z")

</div>

Hi Hendra

hendra kusuma wrote:

> How do I set first column as boolean (shown as checkbox)  
> if I prepare my data with wxgridtablebase?  
> grid.set\_col\_format\_bool does not work

To use custom (eg boolean) renderers and editors with GridTableBase, you  
need to

1. Call Wx::Grid.register\_data\_type to associate a named ‘type’ of cell  
with a particular class of cell, eg

my\_grid.register\_data\_type(‘CheckBoxCell’, Wx::GridCellBooleanRenderer,  
Wx::GridCellBooleanEditor)

1. Provide a get\_type\_name method for your GridTableBase class that  
returns what named ‘type’ to use for any given cell.

This is demonstrated in the gridtablebase.rb sample, where STRING and  
NUMBER types are defined

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:** [March 24, 2010, 3:02am UTC](https://www.ruby-forum.com/t/column-as-boolean-with-wxgridtablebase/185595/3 "2010-03-24T03:02:04Z")

</div>

Thanks Alex. I still cannot get that working  
I dont get what you mean on point 2

here is my case  
I want to display data from database, I give resultset as parameter to  
create this gridtablebase object. First column is to display checkbox,  
while  
next column display data

my class is about like this

class Ds\_anggota\_grup \< Wx::GridTableBase  
attr\_reader :cols, :rows  
def initialize(rs)  
super()  
@data = []

```
@col_names = ["", "Grup", "Id"]

begin
    rs.each do |row|
        each_row = []
        each_row << 0
        each_row << row[:grup]
        each_row << row[:id]
        @data << each_row
    end
    @rows = @data.size
    @cols = (@data[0]).size
rescue
    @rows = 0
    @cols = 0
end

```

end

def get\_number\_rows  
@rows  
end

def get\_number\_cols  
@cols  
end

def get\_value(row, col)  
(@data[row])[col].to\_s  
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)  
@col\_names[col]  
rescue  
“”  
end

def set\_value(row, col, val)  
#puts “Changing the value of cell (#{col}, #{row}) to ‘#{val}’”  
end

end

I assume you told me to add def get\_value\_as\_bool(row, col)  
but I dont get what should I add there  
and if I add empty def it only give error

What is to add there?

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:** [March 24, 2010, 7:57am UTC](https://www.ruby-forum.com/t/column-as-boolean-with-wxgridtablebase/185595/4 "2010-03-24T07:57:02Z")

</div>

On 24/03/2010 02:01, hendra kusuma wrote:

> Thanks Alex. I still cannot get that working  
> I dont get what you mean on point 2
> 
> here is my case  
> I want to display data from database, I give resultset as parameter to  
> create this gridtablebase object. First column is to display checkbox,  
> while next column display data

Your subclass of GridTableBase should provide a get\_type\_name method,  
which accepts (x,y) parameters and returns a string which corresponds to  
a named type defined by your Wx::Grid class. Eg, for your example:

def get\_type\_name(x, y)  
if x == 0  
‘CHECKBOX’  
else  
‘STRING’  
end  
end

As I say, this is shown in the sample, so have a look there for  
‘register\_data\_type’ and ‘get\_type\_name’

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:** [March 25, 2010, 4:12am UTC](https://www.ruby-forum.com/t/column-as-boolean-with-wxgridtablebase/185595/5 "2010-03-25T04:12:49Z")

</div>

Your subclass of GridTableBase should provide a get\_type\_name method,  
which  
accepts (x,y) parameters and returns a string which corresponds to a  
named  
type defined by your Wx::Grid class. Eg, for your example:

> ‘register\_data\_type’ and ‘get\_type\_name’

I cannot find the sample directory in new wxruby-2.0.1 download  
And my old wxruby-doc seems does not have def get\_type\_name example  
I work it somehow, after figure out how the mechanism working, thanks to  
your advise

So, I add 2 register\_data type for the grid

@grid.register\_data\_type(‘CHECKBOX’, Wx::GridCellBoolRenderer.new,  
Wx::GridCellBoolEditor.new)  
@grid.register\_data\_type(‘STRING’, Wx::GridCellStringRenderer.new,  
Wx::GridCellTextEditor.new)

and for grid table base, I follow your code  
it works after I browse the html directory and find that the name is  
Wx::GridCellBoolRenderer  
(your prev said GridCellBoolRenderer, it got me dizzy, but it helps  
really)

Thank you  
As always, you are very helpful  
Regards  
Hendra
