Click here to Skip to main content
15,347,088 members
Home / Discussions / C#
   

C#

 
AnswerRe: how sort datgridview or listview like windows explorer Pin
OriginalGriff21-Mar-22 21:12
mveOriginalGriff21-Mar-22 21:12 
GeneralRe: how sort datgridview or listview like windows explorer Pin
Le@rner22-Mar-22 1:20
MemberLe@rner22-Mar-22 1:20 
GeneralRe: how sort datgridview or listview like windows explorer Pin
Dave Kreskowiak22-Mar-22 1:50
mveDave Kreskowiak22-Mar-22 1:50 
AnswerRe: how sort datgridview or listview like windows explorer Pin
Richard Deeming21-Mar-22 22:32
mveRichard Deeming21-Mar-22 22:32 
GeneralRe: how sort datgridview or listview like windows explorer Pin
Le@rner22-Mar-22 2:00
MemberLe@rner22-Mar-22 2:00 
GeneralRe: how sort datgridview or listview like windows explorer Pin
Le@rner28-Mar-22 1:36
MemberLe@rner28-Mar-22 1:36 
GeneralRe: how sort datgridview or listview like windows explorer Pin
Richard Deeming28-Mar-22 21:45
mveRichard Deeming28-Mar-22 21:45 
AnswerRe: how sort datgridview or listview like windows explorer Pin
RobertSF26-Mar-22 13:32
professionalRobertSF26-Mar-22 13:32 
One solution is to create a class for a "sortable" list and use that as your data source in the DGV. I found this somewhere and have been using it with success. Thanks to the unsung hero who wrote it.
C++
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;

namespace YourNamespace
{
    internal static class NativeMethods
    {
        [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
        internal static extern int StrCmpLogicalW(string aString, string bString);
    }

    internal class SortableList<T> : BindingList<T>
    {
        protected override bool SupportsSortingCore
        {
            get
            {
                return true;
            }
        }

        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
        {
            int modifier = direction == ListSortDirection.Ascending ? 1 : -1;
            if (prop?.PropertyType.GetInterface("IComparable") != null)
            {
                var items = Items.ToList() as List<T>;
                items.Sort(new Comparison<T>((a, b) =>
                {
                    var aVal = prop.GetValue(a).ToString() as string;
                    var bVal = prop.GetValue(b).ToString() as string;
                    return NativeMethods.StrCmpLogicalW(aVal, bVal) * modifier;
                }));
                Items.Clear();
                foreach (var i in items) Items.Add(i);
            }
        }
    }
}

GeneralC# to Mysql Login code Error Pin
N Mohamed rafi21-Mar-22 16:34
MemberN Mohamed rafi21-Mar-22 16:34 
SuggestionRe: C# to Mysql Login code Error Pin
Richard Deeming21-Mar-22 22:26
mveRichard Deeming21-Mar-22 22:26 
AnswerRe: C# to Mysql Login code Error Pin
Richard Deeming21-Mar-22 22:28
mveRichard Deeming21-Mar-22 22:28 
GeneralRe: C# to Mysql Login code Error Pin
N Mohamed rafi21-Mar-22 22:33
MemberN Mohamed rafi21-Mar-22 22:33 
GeneralRe: C# to Mysql Login code Error Pin
Richard Deeming21-Mar-22 22:38
mveRichard Deeming21-Mar-22 22:38 
GeneralRe: C# to Mysql Login code Error Pin
N Mohamed rafi21-Mar-22 22:43
MemberN Mohamed rafi21-Mar-22 22:43 
GeneralRe: C# to Mysql Login code Error Pin
Richard Deeming21-Mar-22 23:03
mveRichard Deeming21-Mar-22 23:03 
GeneralRe: C# to Mysql Login code Error Pin
N Mohamed rafi21-Mar-22 23:20
MemberN Mohamed rafi21-Mar-22 23:20 
GeneralRe: C# to Mysql Login code Error Pin
Richard Deeming21-Mar-22 23:25
mveRichard Deeming21-Mar-22 23:25 
GeneralRe: C# to Mysql Login code Error Pin
N Mohamed rafi21-Mar-22 23:34
MemberN Mohamed rafi21-Mar-22 23:34 
GeneralRe: C# to Mysql Login code Error Pin
Richard Deeming21-Mar-22 23:45
mveRichard Deeming21-Mar-22 23:45 
GeneralRe: C# to Mysql Login code Error Pin
N Mohamed rafi21-Mar-22 23:37
MemberN Mohamed rafi21-Mar-22 23:37 
GeneralRe: C# to Mysql Login code Error Pin
Richard Deeming21-Mar-22 23:44
mveRichard Deeming21-Mar-22 23:44 
GeneralRe: C# to Mysql Login code Error Pin
N Mohamed rafi21-Mar-22 23:47
MemberN Mohamed rafi21-Mar-22 23:47 
RantRe: C# to Mysql Login code Error Pin
Richard Deeming22-Mar-22 0:03
mveRichard Deeming22-Mar-22 0:03 
GeneralRe: C# to Mysql Login code Error Pin
Richard MacCutchan22-Mar-22 0:47
mveRichard MacCutchan22-Mar-22 0:47 
GeneralRe: C# to Mysql Login code Error Pin
N Mohamed rafi22-Mar-22 0:49
MemberN Mohamed rafi22-Mar-22 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.