mgroves

StupidGrid

I recently had need for a ASP.NET WebForms grid that would do something you’d think would be relatively simple:

  • Allow the user to enter ‘rows’ of some set of data (for instance, multiple address/city/state)
  • Allow the user to update/delete all of these rows of data, almost like an Excel sheet
  • Keep that data in a ‘holding pattern’ on the form (via ViewState) until such time they ‘submit’ the form to be persisted, emailed, etc.
  • Not use any databinding, database backing, or session – just ViewState

I looked and I looked, but I couldn’t find anything that quite fit right.  I tried to implement a GridView using ObjectDataSource, wrapped around ViewState, but I couldn’t get it to work.  I tried some jQuery grids, GridView, etc, and these all seem to want to talk to a database (or some persistence somewhere).  I tried a plain old ListView/Repeater, but the dynamically added user controls (i.e. my ‘rows’ of data), disappear after postback.

Since I couldn’t find the perfect fit (it may still be out there), I just created my own (with some serious help from this guy).

stupidgridlogo

Yes, StupidGrid, available on CodePlex.  Why the name?

  1. It doesn’t bind to any database or persistence: once the user leaves the page, StupidGrid stupidly forgets anything the user entered.
  2. I couldn’t find a decent (free) grid to do what I wanted, so as I was looking around for one, I kept saying “I just need a stupid grid!”
  3. It’s stupidly simple to use (or, at least, that’s my goal): just build a user control you want to repeat, “feed” its path to the UserControlPath property, and you’re done.

I’ve just done an official release of version 0.1, which I’m sure has many problems, so I would be ecstatic to hear any opinions, suggestions, patches, criticisms, bug reports, problems, etc.

And if anyone wants to design a better logo or icon, leave a comment or hit me on Twitter.

Bring in the noise, bring in the Func (and Action)

Action and Func are kinda like pointers to methods. I've been using these a lot in a little pet project of mine, and I just wanted to share how awesome they are. They can be passed around, put into a collection, and invoked multiple times. Action (and Action<T> and Action<T1,T2>, Action<T1,T2,T3>) can 'point' to a method that doesn't return anything (i.e. a 'void' method). Like so:

    public class Class1
{
public void SomeMethod()
{
// do stuff
}

public void Example()
{
Action a = SomeMethod;
a.Invoke();
}
}

It's a silly example, but "a.Invoke();" is the same as just using "SomeMethod();" Similarly, if SomeMethod had a parameter (or two or three), then you could do this:

    public class Class1
{
public void SomeMethod(int a, string b)
{
// do stuff
}

public void Example()
{
Action<int,string> a = SomeMethod;
a.Invoke(1,"stuff");
}
}

So far, so awesome. Func is the same thing, except it 'points' to a method that can return something. Behold:

    public class Class1
{
public double SomeMethod(int a, string b)
{
// do stuff
return 0.0;
}

public void Example()
{
Func<int,string,double> f = SomeMethod;
double d = f.Invoke(5, "stuff");
}
}

Note that the 'result' type goes in the last slot in the Func: Func<param1,param2,result>. That seems backwards to me, but oh well. Now mix in some lambas for anonymous action:

        public void Example()
{
Action a = () => {
// do stuff
};
a.Invoke();
}

Now you create anonymous methods, stick them in a list, or a stack, or something, and go through and invoke them (or not invoke them) as you like.

These are contrived examples that aren't very useful. What I'm using them for is returning a collection of private methods, depending on various conditions. The user can then pick which one(s) to use from that list. The ones the user picks get placed into a stack and then invoked in LIFO order (this is part of a game).

Anyway, I think Action and Func are really cool parts of C#.