ListView extension I thought I’d share…

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.

Print | posted @ Wednesday, July 22, 2009 10:51 PM

Comments on this entry:

Gravatar # re: ListView extension I thought I’d share…
by James Curran at 7/24/2009 9:24 AM

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.
Gravatar # re: ListView extension I thought I’d share…
by Samer Paul at 7/24/2009 2:41 PM

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)
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: