Archive for Agosto 14th, 2008

Comparer dynamique pour tri List

CodeKeep C# Feed Agosto 14th, 2008

Description: Comparer dynamique pour tri sur plusieurs attributs dans GridView avec liste

Link: http://www.codekeep.net/snippets/b17d6804-6f6c-4474-926a-6abf3cf6dd62.aspx

    /// <summary>
    /// CplProduitComparer is used to sort the generic collection of the CplProduit class
    /// </summary>
    public class CplProduitComparer : IComparer<CplProduit>
    {
        #region Constructor
        public CplProduitComparer(string p_propertyName)
        {
            //We must have a property name for this comparer to work
            this.PropertyName = p_propertyName;
        }
        #endregion

        #region Property
        private string _propertyName;

        public string PropertyName
        {
            get { return _propertyName; }
            set { _propertyName = value; }
        }
        #endregion


        #region IComparer<CplProduit> Members

        /// <summary>
        /// This comparer is used to sort the generic comparer
        /// The constructor sets the PropertyName that is used
        /// by reflection to access that property in the object to 
        /// object compare.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int Compare(CplProduit x, CplProduit y)
        {
            Type t = x.GetType();
            PropertyInfo val = t.GetProperty(this.PropertyName);
            if (val != null)
            {
                return Comparer.DefaultInvariant.Compare(val.GetValue(x, null), val.GetValue(y, null));
            }
            else
            {
                throw new Exception(this.PropertyName + " is not a valid property to sort on.  It doesn't exist in the Class.");
            }
        }

        #endregion
    }

Sort List sans comparer avec delegate

CodeKeep C# Feed Agosto 14th, 2008

Description: Pas besoin d'implémenter IComparer

Link: http://www.codekeep.net/snippets/ce03c477-aeb8-4ca2-ab81-5499bf32141a.aspx

this.listResultat.Sort(delegate(CplProduit x, CplProduit y) { return x.DPrime.CompareTo(y.DPrime); });

Comparer Simple

CodeKeep C# Feed Agosto 14th, 2008

Description: sur un oList.sort(), Tri pas défaut sur un attribut Pas oublier d'hériter de IComparable

Link: http://www.codekeep.net/snippets/ab16a606-e85c-47a5-b86e-2195f07ab4c2.aspx

        public int CompareTo(Object obj)
        {
            if (obj.GetType() != typeof(CplProduit))
                throw new ArgumentException("Impossible de comparer");
            return this.DPrime.CompareTo(((CplProduit)obj).DPrime);
        }

List Datatable Contents

CodeKeep C# Feed Agosto 14th, 2008

Description: List Datatable Contents

Link: http://www.codekeep.net/snippets/b6ca25f1-d95d-4d2a-8b7a-b9569f93c799.aspx

        foreach (DataTable table in ds.Tables)
        {
            Response.Write("<b>" + table.ToString() + "</b><br>");
            foreach (DataRow row in table.Rows)
            {
                Response.Write("<b>" + row.ToString() + "</b><br>");
                foreach (DataColumn col in table.Columns)
                {
                    Response.Write(row[col].ToString() + ", ");
                }
                Response.Write("<br>");
            }
        }