使所有datagridview列可排序

时间:2016-01-22 10:57:30

标签: c# datagridview

我有一个带有datagridview的Windows表单。

理想情况:

用户点击九列中的任何一列,程序会对所有数据进行排序,如果点击的列包含数字,我希望顶部的数字最小。如果单击的列包含一个字符串,我希望按字母顺序排序(A-Z)。

我现在拥有的东西:

我在Stack Overflow上看到一个旧问题,OP在点击“a”标题时如何对datagridview进行排序。与我的不同之处在于我希望我的datagridview可以通过九列中的任何一列进行排序。

我有这个代码,从我发现的问题中偷走了:

dataGridView2.DataSource = listPlayers.Select(s => new { voornaam = s.Voornaam, 
                                                        Achternaam = s.Achternaam, 
                                                        positie = s.Positie, 
                                                        Nationaltieit = s.Nationaliteit, 
                                                        Leeftijd = s.Age, 
                                                        Aanval = s.Aanval, 
                                                        Verdediging = s.Verdediging, 
                                                        Gemiddeld = s.Gemiddeld, 
                                                        waarde = s.TransferWaarde })
                                   .OrderBy(s => s.Achternaam)
                                   .ToList();

foreach(DataGridViewColumn column in dataGridView2.Columns)
    {
        dataGridView2.Columns[column.Name].SortMode =
                                  DataGridViewColumnSortMode.Automatic;
    }

这只允许用户在点击九列中的一列时按“Achternaam”排序。我想要的是当用户点击Nationaliteit列时,数据按An排在最前面。等等每列

这是列表播放器列表:

namespace SimulatorSimulator
{
    class SpelerData
    {
        public string Voornaam { get; set; }
        public string Achternaam { get; set; }
        public string Positie { get; set; }
        public string Nationaliteit { get; set; }
        public int Age { get; set; }
        public int Aanval { get; set; }
        public int Verdediging { get; set; }
        public int Gemiddeld { get; set; }
        public string TransferWaarde { get; set; }
    }
}

在主要课程中:

 List<SpelerData> listPlayers = new List<SpelerData>();

一些虚拟数据:

Romelu;Lukaku;Aanvaller;Belgie;22;87;12;50;41.000.000,00    
Raheem ;Sterling;Aanvaller;Engeland;21;84;30;57;35.000.000,00    
Zlatan ;Ibrahimovic;Aanvaller;Zweden;34;87;21;54;34.500.000,00

3 个答案:

答案 0 :(得分:6)

我认为最简单的方法是将数据放在Database表中。这样,您就可以将其用作data source的{​​{1}},并且您可以通过点击标题列轻松进行排序。

另一种方法是使用SortableBindingListarticle),正如另一个答案所暗示的那样。

但如果两个选项都不在您的选择中,我可以想到的下一个最简单的方法是从dataGridView2创建事件,然后您可以通过使用ColumnHeaderMouseClick来相应地列出您的排序正确&#34;映射&#34; (字典)到你准备好的e.ColumnIndex

因此,在表单加载中,您可以执行以下操作:

IEnumerable<SpelerData>

然后在Dictionary<int, IEnumerable<SpelerData>> queryDict = new Dictionary<int, IEnumerable<SpelerData>>(); //Prepare a dictionary of query private void form_load(object sender, EventArgs e) { dataGridView2.DataSource = listPlayers.OrderBy(x => x.Achternaam).ToList(); queryDict.Add(0, listPlayers.OrderBy(x => x.Voornaam)); queryDict.Add(1, listPlayers.OrderBy(x => x.Achternaam)); queryDict.Add(2, listPlayers.OrderBy(x => x.Positie)); queryDict.Add(3, listPlayers.OrderBy(x => x.Nationaliteit)); queryDict.Add(4, listPlayers.OrderBy(x => x.Age)); queryDict.Add(5, listPlayers.OrderBy(x => x.Aanval)); queryDict.Add(6, listPlayers.OrderBy(x => x.Verdediging)); queryDict.Add(7, listPlayers.OrderBy(x => x.Gemiddeld)); queryDict.Add(8, listPlayers.OrderBy(x => x.TransferWaarde)); } 事件中,只需执行:

ColumnHeaderMouseClick

你会得到你想要的行为。

请注意,由于private void dataGridView2_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { dataGridView2.DataSource = queryDict[e.ColumnIndex].ToList(); } 延迟执行,IEnumerable的早期准备工作根本不会对性能产生影响。您需要添加的唯一内容是form_Load中的9行代码,用于准备字典和Dictionary事件中的1行代码。除了前面提到过的两个我能想到的解决方案之外,这是最简单的解决方案。

答案 1 :(得分:4)

您可以使用SortableBindingList

SortableBindingList<T> list = new SortableBindingList<T>();

//Add items to list

dataGridView.DataSource = list ;

这将允许在单击列标题

时进行排序
public class SortableBindingList<T> : BindingList<T>
{
    private readonly Dictionary<Type, PropertyComparer<T>> comparers;
    private bool isSorted;
    private ListSortDirection listSortDirection;
    private PropertyDescriptor propertyDescriptor;

    public SortableBindingList()
        : base(new List<T>())
    {
        this.comparers = new Dictionary<Type, PropertyComparer<T>>();
    }

    public SortableBindingList(IEnumerable<T> enumeration)
        : base(new List<T>(enumeration))
    {
        this.comparers = new Dictionary<Type, PropertyComparer<T>>();
    }

    protected override bool SupportsSortingCore
    {
        get { return true; }
    }

    protected override bool IsSortedCore
    {
        get { return this.isSorted; }
    }

    protected override PropertyDescriptor SortPropertyCore
    {
        get { return this.propertyDescriptor; }
    }

    protected override ListSortDirection SortDirectionCore
    {
        get { return this.listSortDirection; }
    }

    protected override bool SupportsSearchingCore
    {
        get { return true; }
    }

    protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
    {
        List<T> itemsList = (List<T>)this.Items;

        Type propertyType = property.PropertyType;
        PropertyComparer<T> comparer;
        if (!this.comparers.TryGetValue(propertyType, out comparer))
        {
            comparer = new PropertyComparer<T>(property, direction);
            this.comparers.Add(propertyType, comparer);
        }

        comparer.SetPropertyAndDirection(property, direction);
        itemsList.Sort(comparer);

        this.propertyDescriptor = property;
        this.listSortDirection = direction;
        this.isSorted = true;

        this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
    }

    protected override void RemoveSortCore()
    {
        this.isSorted = false;
        this.propertyDescriptor = base.SortPropertyCore;
        this.listSortDirection = base.SortDirectionCore;

        this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
    }

    protected override int FindCore(PropertyDescriptor property, object key)
    {
        int count = this.Count;
        for (int i = 0; i < count; ++i)
        {
            T element = this[i];
            if (property.GetValue(element).Equals(key))
            {
                return i;
            }
        }

        return -1;
    }
}

答案 2 :(得分:1)

如果您愿意使用反射,您可以执行以下操作。

注意:我假设您正在使用DataGridView.ColumnHeaderMouseClick Event,但这并未改变此方法的核心。关键是您需要动态识别表示要OrderBy列的名称的字符串值。如果你真的需要/想要,你可以对这种关联进行硬编码。

private void dataGridView2_ColumnHeaderMouseClick(
    object sender, DataGridViewCellMouseEventArgs e)
{
    ...
    var sortCol = dataGridView2.Columns[e.ColumnIndex];
    var colName = sortCol.Name;

    dataGridView2.DataSource = listPlayers.Select(s => new { voornaam = s.Voornaam, Achternaam = s.Achternaam, positie = s.Positie, Nationaltieit = s.Nationaliteit, Leeftijd = s.Age, Aanval = s.Aanval, Verdediging = s.Verdediging, Gemiddeld = s.Gemiddeld, waarde = s.TransferWaarde })
                                          .OrderBy(s => typeof(SpelerData).GetProperty(colName))
                                          .ToList();
    ...
}
相关问题