Geeks With Blogs
Create Fun Things - Samer's Developer Blog Development Blog for C#, .NET and Obj-C

I use the ListView pretty much exclusively for all my databinding needs, because, well, it does everything. One of the common things I do over and over in my backend is something like this:

myListView.DataSource = myDataSource;
myListView.DataBind();

I wanted to basically set the datasource, and bind it all in one line of code (because I’m lazy). So I wrote up this extension method:

 

public static ListView QuickDataBind(this ListView myListView, object myDataSource)
    {
        myListView.DataSource = myDataSource;
        myListView.DataBind();
        return myListView;
    }

Now whenever I want to set a datasource and bind it, I simply write:

myListView.QuickDataBind(myDataSource);

 

and it’s done!

Just a little extension I thought might be useful for “aps.netters” who use the ListView a lot, and databind from the code-behind.

Posted on Wednesday, July 22, 2009 10:51 PM | Back to top


Comments on this post: ListView extension I thought I’d share…

# re: ListView extension I thought I’d share…
Requesting Gravatar...
Why limit yourself?

public static T QuickDataBind<T>(this T myListView, object myDataSource) where T: BaseDataBoundControl
{ ... }

The body & calling is identical, except now, it can be using with any control that uses a DataSource.
Left by James Curran on Jul 24, 2009 9:24 AM

# re: ListView extension I thought I’d share…
Requesting Gravatar...
Thanks! I wrote it a while ago and never thought about extending it to all databinding controls. (I pretty much exclusively use the ListView so it never really came up)
Left by Samer Paul on Jul 24, 2009 2:41 PM

Your comment:
 (will show your gravatar)


Copyright © samerpaul | Powered by: GeeksWithBlogs.net