Weblogs Código

RecorteX

73 usuarios y 243 recortes de código
Usuario

Contraseña
Crear cuenta
asp csharp css fechas Flash html imagenes java javascript linux mysql oracle php sql SQLServer url vbnet web xhtml xml
Top Usuarios
fjmp76 (42)
albin (31)
josuebasurto (31)
juanjo (30)
pedrocaro (9)
stanz (8)
AndiXTRM (7)
Joserra (7)
miiglesi (6)
aplyca (5)
Top Etiquetas
php (38)
vbnet (28)
javascript (26)
html (25)
csharp (25)
java (24)
sql (18)
xml (13)
mysql (13)
css (10)
xhtml (7)
Flash (7)
imagenes (7)
linux (6)
fechas (6)
asp (6)
oracle (5)
url (5)
web (5)
SQLServer (5)

SortableBindingList para Winforms

SortableBindingList para Winforms

hay que agregar el evento ColumnHeaderMouseClick al dataGridView

 
        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            SortOrder order = this.dataGridView1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection;
            ListSortDirection _direction = (order == SortOrder.Descending) ? ListSortDirection.Descending : ListSortDirection.Ascending;
            this.dataGridView1.Sort(dataGridView1.Columns[e.ColumnIndex], _direction);
        }
 

Y la lista que usemos será la SortableBindingList del objeto

 
      public class SortableBindingList<T> : BindingList<T>
    {
        private ArrayList sortedList;
        private bool isSortedValue;
 
        public SortableBindingList()
        {
        }
 
        public SortableBindingList(IList<T> list)
        {
            foreach (object o in list)
            {
                this.Add((T)o);
            }
        }
 
        protected override bool SupportsSortingCore
        {
            get { return true; }
        }
 
 
        protected override bool IsSortedCore
        {
            get { return isSortedValue; }
        }
 
        ListSortDirection sortDirectionValue;
        PropertyDescriptor sortPropertyValue;
 
        protected override void ApplySortCore(PropertyDescriptor prop,
            ListSortDirection direction)
        {
            sortedList = new ArrayList();
 
            Type interfaceType = prop.PropertyType.GetInterface("IComparable");
 
            if (interfaceType == null && prop.PropertyType.IsValueType)
            {
                Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType);
 
                if (underlyingType != null)
                {
                    interfaceType = underlyingType.GetInterface("IComparable");
                }
            }
 
            if (interfaceType != null)
            {
                sortPropertyValue = prop;
                sortDirectionValue = direction;
 
                IEnumerable<T> query = base.Items;
                if (direction == ListSortDirection.Ascending)
                {
                    query = query.OrderBy(i => prop.GetValue(i));
                }
                else
                {
                    query = query.OrderByDescending(i => prop.GetValue(i));
                }
                int newIndex = 0;
                foreach (object item in query)
                {
                    this.Items[newIndex] = (T)item;
                    newIndex++;
                }
                isSortedValue = true;
                this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
 
            }
            else
            {
                throw new NotSupportedException("Cannot sort by " + prop.Name +
                    ". This" + prop.PropertyType.ToString() +
                    " does not implement IComparable");
            }
        }
 
        protected override PropertyDescriptor SortPropertyCore
        {
            get { return sortPropertyValue; }
        }
 
        protected override ListSortDirection SortDirectionCore
        {
            get { return sortDirectionValue; }
        }
 
    }
 
Identifícate para dejar un comentario.
Proyecto: Juanjo Navarro, 2006 - Diseño: Albin