Groups > Asp .Net > Advanced ASP.NET Architecture > Re: How to write a BLL for generic GridView sorting that applies for any GridViews




How to write a BLL for generic GridView sorting that applies
for any GridViews

How to write a BLL for generic GridView sorting that applies for any GridViews
Mon, 31 Mar 2008 21:00:04 +000
I have a number of Web pages that contain GridView in each (each GridView has
different columns and is pupulated from different tables.) I want to write a
generalized BLL that handled sorting of any Gridview in these Web Pages. I know
many examples of BLL handling GridView sorting through ObjectDataSource; but the
BLL in these example handles each GridView (and its clumns) individually.  I
don't want to handle individual columns of each GridView in the BLL for sorting
purposes. How can I do this? Can I pass the GridView ID to the BLL so it can
handle the sorting within BLL? 

Appreciate any suggestions. Thanks in advance.

sg2000
Post Reply
Re: How to write a BLL for generic GridView sorting that applies for any GridViews
Tue, 1 Apr 2008 12:58:24 +0000
You can sort your data before you bind it to your gridview when you retrieve the
data based on some parameters.

GridView1.DataSource = BL.GetData(sortfield, sortdirection);
GridView1.DataBind();

You will then retrieve the data from the datalayer, sort it on the given
parameters and return the sorted data used as datasource.
Post Reply
Re: How to write a BLL for generic GridView sorting that applies for any GridViews
Tue, 1 Apr 2008 17:12:32 +0000
Thanks littlefool for the response. However, how can I access GridView1 (which
is defined in the aspx) from within a separate class (the BLL)? Appreciate any
suggestions. Thanks.

sg2000
Post Reply
Re: How to write a BLL for generic GridView sorting that applies for any GridViews
Wed, 2 Apr 2008 09:35:42 +0000
I would not do this. The code I showed was viewed from the aspx point of view.
The BL.GetData is the business layer that returns an object that can be used as
the DataSource, such as a List or DataSet. 

So, the Business layer would look something like this:

        public List<MyObject> GetData(string sortfield, string
sortdirection)
        {
            DataLayer dl = new DataLayer();
            // assume the datalayer returns a generic list. It can also be a
DataSet or any other data collection you use
            List<MyObject> data = dl.GetData();
            // you should add the sort logic here depending on what data source
you return
            // for business objects you probably need to have an IComparer class
that will 
            //  compare all your objects depending on your parameters.
            data.Sort(); // sorting the data collection
            return data;
        }
Post Reply
Re: How to write a BLL for generic GridView sorting that applies for any GridViews
Wed, 2 Apr 2008 16:39:22 +0000
Thanks littlefool for the BLL code. Yes, this would work. My question is: if you
use ObjectDataSource in your aspx to populate your gridview, how can you send
the sortfield and sortdirection as parameters of the GetData?

Thanks again,

sg2000
Post Reply
<< Previous 1 2 Next >>
( Page 1 of 2 )
about | contact