CodeSmith Community
Your Code. Your Way. Faster!

Sort on IDSource column ?

Latest post 10-28-2007 10:25 AM by MikeBosch. 8 replies.
  • 11-13-2006 5:27 PM

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 186
    • Points 4,716

    Sort on IDSource column ?

    Hi!

    I would like to know whether there is a trick to sort a GridView based on a column that contains IDSource information. For example, if I have a GridView with a column populated using Eval("AddressIDSource.StreetName"). Is it possible to sort the GridView based on the Street Name?

    Cheers!

     JF
     

    Filed under: ,
    • Post Points: 35
  • 11-14-2006 3:50 PM In reply to

    Re: Sort on IDSource column ?

    Hi,

    No, unfortunately the ASP.net GridView does not support sorting on complex objects. You could workaround this situation by creating a property on your object for the StreetName

    [Bindable]
    public string AddressIdStreetName
    {
      get{ return AddressIDSource.StreetName; }
      set { AddressIDSource.StreetName = value; }
    }


    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 65
  • 11-14-2006 3:58 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 186
    • Points 4,716

    Re: Sort on IDSource column ?

    Hi!

    Thanks Robert! I had thought about this, but it would be a last resort solution for us, since we use look-up tables everywhere and we need this in every GridView.

    Do Strongly Typed Data Sources support sorting on complex objects? The Data Source would need to perform a JOIN internally I guess. Is this supported? I could then apply the sorting on the Data Source and rebind the GridView.

    Cheers!

    JF
     

    Filed under: ,
    • Post Points: 65
  • 11-14-2006 4:08 PM In reply to

    Re: Sort on IDSource column ?

    It's the actual GridView that's to blame and not the bound data.  I do believe the winforms gridview allows it so it's just a matter of changing the gridview so that supports it.

    On 11/14/06, velum <bounce-velum@codesmithsupport.com> wrote:

    Hi!

    Thanks Robert! I had thought about this, but it would be a last resort solution for us, since we use look-up tables everywhere and we need this in every GridView.

    Do Strongly Typed Data Sources support sorting on complex objects? The Data Source would need to perform a JOIN internally I guess. Is this supported? I could then apply the sorting on the Data Source and rebind the GridView.

    Cheers!

    JF
     






    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 5
  • 11-14-2006 4:22 PM In reply to

    • jcteague
    • Top 10 Contributor
    • Joined on 03-10-2005
    • Austin, Tx
    • Posts 442
    • Points 10,925

    Re: Sort on IDSource column ?

    Reply |Contact |Answer
    Another option would be to hook up the Sorting event of the GridView and sort your collection using the Comparer delegate or a Comparator class.
     
    something like:
    private static int ChildPropertyComparer(Entity x, Entity y){
      return x.AddressIDSource.StreetName.CompareTo(y.AddressIDSource.StreetName);
    }
    list.Sort( ChildPropertyComparer);
     
    You could put the Comparer method in a utility class and reuse in all of your pages.
     
    For more info on sorting check out:
     
     

     
    On 11/14/06, velum <bounce-velum@codesmithsupport.com > wrote:

    Hi!

    Thanks Robert! I had thought about this, but it would be a last resort solution for us, since we use look-up tables everywhere and we need this in every GridView.

    Do Strongly Typed Data Sources support sorting on complex objects? The Data Source would need to perform a JOIN internally I guess. Is this supported? I could then apply the sorting on the Data Source and rebind the GridView.

    Cheers!

    JF
     





    Thanks, John Teague ------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------

    • Post Points: 35
  • 12-18-2006 11:27 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 186
    • Points 4,716

    Re: Sort on IDSource column ?

    Hi!

    In EntityPropertyComparer.cs, we find the following method:

            public int Compare(object x, object y)
            {
                object a = x.GetType().GetProperty(PropertyName).GetValue(x, null);
                object b = y.GetType().GetProperty(PropertyName).GetValue(y, null);

                if ( a != null && b == null )
                    return 1;

                if ( a == null && b != null )
                    return -1;

                if ( a == null && b == null )
                    return 0;

                return ((IComparable)a).CompareTo(b);
            }

    Would it be possible to modify this method's implementation so that it would possible to use a PropertyName like 'CustomerIDSource.CustomerName'. I guess, what would have to be done is to split the Property name on the period character and find the type and value of the last object (CustomerName in this case). I don't have any experience with reflexion. Sorry for my ignorence.

    If such a comparer would be feasible, would it be possible then to get a GridView to use it?

    Cheers!

    JF
     

    Filed under: ,
    • Post Points: 5
  • 01-13-2007 6:19 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 186
    • Points 4,716

    Re: Sort on IDSource column ?

    What about replacing the above routine by the two following ones, in order to support complex objects?

            public object getObj(object x)
            {
                object a = string.Empty;
                string[] PropertyParts;
                char[] delimiterChars = { '.' };
                PropertyParts = PropertyName.Split(delimiterChars);
                foreach (string propertyPart in PropertyParts)
                {
                    a = x.GetType().GetProperty(propertyPart).GetValue(x, null);
                    x = a;
                }

                return a;
            }

            /// <summary>
            /// Compares 2 objects by their properties, given on the constructor
            /// </summary>
            /// <param name="x">First value to compare</param>
            /// <param name="y">Second value to compare</param>
            /// <returns></returns>
            public int Compare(object x, object y)
            {
                //object a = x.GetType().GetProperty(PropertyName).GetValue(x, null);
                //object b = y.GetType().GetProperty(PropertyName).GetValue(y, null);
                object a = getObj(x);
                object b = getObj(y);

                if ( a != null && b == null )
                    return 1;

                if ( a == null && b != null )
                    return -1;

                if ( a == null && b == null )
                    return 0;

                return ((IComparable)a).CompareTo(b);
            }

    Cheers!

    JF
     

    • Post Points: 35
  • 10-10-2007 10:36 AM In reply to

    • iatek
    • Top 500 Contributor
    • Joined on 08-03-2007
    • Posts 18
    • Points 270

    Re: Sort on IDSource column ?

    Hi,

    Has anyone successfully implemented this? I'm wondering how to get my GridView to use the EntityPropertyComparer. Thanks for any help.

    Skelly
    • Post Points: 5
  • 10-28-2007 10:25 AM In reply to

    • MikeBosch
    • Top 500 Contributor
    • Joined on 06-04-2007
    • Miami, FL
    • Posts 16
    • Points 495

    Re: Sort on IDSource column ?

    I tried adding the [Bindable] attribute but it still did not work.  There are some overloads to the constructor, what should be in the overloads to support this?

    • Post Points: 5
Page 1 of 1 (9 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems