Column as boolean with wxgridtablebase

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

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

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs