0

I have written an Extension Method off of DataGridView called HideColumns.

public static class Extensions
{
    public static void HideColumns(this DataGridView dataGridView, params string[] columnNames)
    {
        foreach (string str in columnNames)
        {
            if (dataGridView.Columns[str] != null)
            {
                dataGridView.Columns[str].Visible = false;
            }
        }
    }

}

I pass my grid into an IronRuby script as a variable called main_grid

When my script calls main_grid.HideColumns("FirstName","LastName") the script blows up with Error in Script undefined method 'HideColumns' for System.Windows.Forms.DataGridView:System::Windows::Forms::DataGridView

The extension methods seem to work okay from C#. What gives?

BuddyJoe
  • 69,735
  • 114
  • 291
  • 466

3 Answers3

2

FWIW, IronRuby 1.1 (needs .net 4) provides the using_clr_extensions method -- as noted in the release notes this activates all extension methods defined on classes defined in a given namespace, regardless of the assembly they are defined in; assemblies loaded in the future that define extension methods in the activated namespace will automatically appear on the correct types, like this:

load_assembly "System.Core"
using_clr_extensions System::Linq

# ...

products.
  where(lambda { |p| p.units_in_stock == 0 }).
  each { |x| puts x.product_name }

The release notes also point at a whole set of examples at http://github.com/ironruby/ironruby/blob/master/Languages/Ruby/Samples/Linq/101samples.rb

Steve Gilham
  • 11,237
  • 3
  • 31
  • 37
1

The extension method is just syntatic sugar, you will need to call it as:

Extensions.HideColumns(main_grid, "FirstName", "LastName")

alternatively create a new class in C# which derives from DataGridView and add the method:

public class DataGridViewExt : DataGridView  
{
    public void HideColumns(params string[] columnNames)
    {
        foreach (string str in columnNames)
        {
            if (this.Columns[str] != null)
            {
                this.Columns[str].Visible = false;
            }
        }
    }        
}

and use this class rather than the System.Windows.Forms class on your form.

JDunkerley
  • 12,355
  • 5
  • 41
  • 45
  • Can I extend the DataGridView (add sugar) from the Ruby side in 3.5? How do I go about this? – BuddyJoe Sep 23 '09 at 14:13
  • Dont believe so, the easiest way would be to derive a class from DataGridView in C# and add the extension method in as a normal instance method (will update answer) – JDunkerley Sep 23 '09 at 14:19
  • Hmmm. Ok. I'll try that. +1 - I always get a little nervous about extending classes with visual representations. Do you know much about .NET 4.0 and IronRuby interop? Will I have other options then? – BuddyJoe Sep 23 '09 at 14:38
  • Don't think it would help that way, I think the dynamic stuff is more for C# calling onto dynamic languages rather than the otherway round. – JDunkerley Sep 23 '09 at 14:56
0

Since you mentioned it in the comments to JDunkeryly's answer, here's how you'd extend the grid from the ruby side. Just open the class and add a method (only works from the ruby side).

class System::Windows::Forms::DataGridView
  def hide_columns(*columnNames)
    column_names.each do |cn|
      self.columns[cn].visible = false
    end
  end
end

As far as the suggestion to use the extension method directly, the params keyword is painful to IronRuby. You need to build a typed array with your arguments and pass it. You can't just wrap your ruby strings in a ruby array. I've pulled this off earlier today in a blog post. But if you have a smoother way to handle that, please let me know.

Ball
  • 2,591
  • 3
  • 19
  • 26