C# DataGridView sorting with Generic List as underlying source

前端 未结 9 1813
情书的邮戳
情书的邮戳 2020-12-05 03:22

I\'m using a Windows Forms DataGridView to display a generic list of MyObject objects.

First of all I wrap this collection into a

相关标签:
9条回答
  • 2020-12-05 03:53

    A good Solution in this article "Presenting the SortableBindingList": http://www.timvw.be/2007/02/22/presenting-the-sortablebindinglistt/

    0 讨论(0)
  • 2020-12-05 03:54

    I find it hard to believe the grid doesn't provide basic sorting out of the box, no code needed. After all, it is pretty silly to have to handle a header click event and call DataGridView.Sort indicating the column (determined by what was clicked, tracked by the grid) and the sort direction (determined by current sort state, tracked by the grid).

    Why isn't there simply a SortMode or an AllowUserToSort property that does exactly the same thing by default?

    I've bound my grid to a List and the properties I've mapped columns to are all basic types like string, int, DateTime and so on. All of which are IComparable. So why on earth should I need to write even one line of code? Especially considering that the documentation reads:

    By default, users can sort the data in a DataGridView control by clicking the header of a text box column.

    MSDN

    That's the Framework 3.0 doc, and I'm targeting 3.5, but "other versions" all refer to versions of Visual Studio, not versions of the Framework. What on earth is going on here Microsoft?!?

    0 讨论(0)
  • 2020-12-05 03:56

    Here is a simpler solution to sort by column using Reflection and Linq. dataGridView1's DataSource is set to compareList which is declared as:

        private List<CompareInfo> compareList;
    
    
        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strColumnName = dataGridView1.Columns[e.ColumnIndex].Name;
            SortOrder strSortOrder = getSortOrder(e.ColumnIndex);
    
            if (strSortOrder == SortOrder.Ascending)
            {
                compareList = compareList.OrderBy(x => typeof(CompareInfo).GetProperty(strColumnName).GetValue(x, null)).ToList();
            }
            else
            {
                compareList = compareList.OrderByDescending(x => typeof(CompareInfo).GetProperty(strColumnName).GetValue(x, null)).ToList();
            }
            dataGridView1.DataSource = compareList;
            dataGridView1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = strSortOrder;
        }
    
        private SortOrder getSortOrder(int columnIndex)
        {
            if (dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection == SortOrder.None ||
                dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection == SortOrder.Descending)
            {
                dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                return SortOrder.Ascending;
            }
            else
            {
                dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection = SortOrder.Descending;
                return SortOrder.Descending;
            }
        }
    
    
    public class CompareInfo
    {
        public string FileName { get; set; }
    
        public string UAT_Folder { get; set; }
    
        public string UAT_Path
        {
            get { return UAT_Folder + FileName; }
        }
    
        public string PROD_Folder { get; set; }
    
        public string PROD_Path
        {
            get { return PROD_Folder + FileName; }
        }
    }
    
    0 讨论(0)
提交回复
热议问题