It’s going to take a lot of work to let you data bind against a native Ruby array, or a native Ruby array of objects. So how about some helper methods that takes an array of Ruby objects and converts it into a DataTable? Or a helper method that converts a Ruby array into an ArrayList? (Once generics are working I can marshal it into an array or a List<T>).
While this is not the most efficient way to do things since it involves copying data in memory, would it be a useful workaround in the short term?
Update: I implemented this tonight after fixing some bugs in my overload resolution algorithm. Works like a charm.
def marshal_to_data_table(array)
dt = DataTable.new
raise 'array must have at least one element' if array.length < 1
schema = array[0].members
schema.each { |column_name| dt.Columns.Add(column_name) }
array.each do |element|
row = dt.NewRow
schema.each do |column_name|
row[column_name] = element.send column_name
end
dt.Rows.Add(row)
end
dt
end
2 archived reader comments
Justin (2006/02/17)
Is there a way you can have the array of ruby objects expose the IList or IBindingList interface? I haven’t done much with .NET 2.0 yet, but I did read about the BindingSource object. Is there some way to talk to that easily? Honestly, I had totally forgotten about data binding but there are some really cool possibilities there. What if I could set my DataSource to a block? How about a combo box with a DisplayMember and ValueMember bound a ruby string that has embedded expressions? Seriously cool possibilities!
John Lam (2006/02/17)
BindingSource is just an intermediary object. You still bind your data source to a BindingSource, and you bind you controls to the BindingSource. I could make it so that you can data bind natively to Ruby objects. But that now requires that I generate a CLR shim class to talk to the Ruby object (which is a lot more work than delegates, which is how I make it two-way communications happen today). This is definitely a goal sometime down the road, just not sure when.