单击列标题上的C#排序数据网格视图

本文关键字:数据 数据网 网格 视图 排序 标题 单击 | 更新日期: 2023-09-27 18:13:49

:(我有一个数据网格视图,我按列表填充它(我从两个文本文件中获得数据(,但当我尝试单击列标题时(我尝试使用所有列标题(,我无法对数据网格视图数据进行排序。这是我的代码:

 public class Data1
    {
        public string Campionato { get; set; }
        public string Data { get; set; }
        public string Home { get; set; }
        public string Away { get; set; }
        public int HSFT { get; set; }
        public int ASFT { get; set; }
        public int HSHT { get; set; }
        public int ASHT { get; set; }
        public int HSSH { get; set; }
        public int ASSH { get; set; }
    }
    public class Data2 
    {
        public string Home { get; set; }
        public string Away { get; set; }
        public int HSFT { get; set; }
        public int ASFT { get; set; }
        public string HODD { get; set; }
        public string XODD { get; set; }
        public string AODD { get; set; } //no name in sample
        public string Data { get; set; }
        public string RisFin { get; set; } //no name in sample
        public string Over05SH { get; set; }
        public string Over05HT { get; set; }
        public string Over15HT { get; set; }
        public string Over05FT { get; set; }
        public string Over15FT { get; set; }
        public string Over25FT { get; set; }
        public string Over35FT { get; set; }
        public string Over45FT { get; set; }

    }
    public class CombinedData 
    {
        public string Campionato { get; set; }
        public string Data { get; set; }
        public string Home { get; set; }
        public string Away { get; set; }
        public int HSFT { get; set; }
        public int ASFT { get; set; }
        public int HSHT { get; set; }
        public int ASHT { get; set; }
        public int HSSH { get; set; }
        public int ASSH { get; set; }
        public string HODD { get; set; }
        public string XODD { get; set; }
        public string AODD { get; set; } //some name
        public string RisFin { get; set; } //no name in sample
        public string Over05SH { get; set; }
        public string Over05HT { get; set; }
        public string Over15HT { get; set; }
        public string Over05FT { get; set; }
        public string Over15FT { get; set; }
        public string Over25FT { get; set; }
        public string Over35FT { get; set; }
        public string Over45FT { get; set; }
    }

var data1 = File.ReadAllLines("read" + campionatoselezTxt.Text + "stats.txt").ToList();
        var data2 = File.ReadAllLines("read" + campionatoselezTxt.Text + "bex.txt").ToList();

        var dataList1 = new List<Data1>();
        foreach (var data in data1)
        {
            var columns = data.Split(';'); 
            dataList1.Add(new Data1
            {
                Campionato = columns[0],
                Data = columns[1],
                Home = columns[2],
                Away = columns[3],
                HSFT = int.Parse(columns[4]),
                ASFT = int.Parse(columns[5]),
                HSHT = int.Parse(columns[6]),
                ASHT = int.Parse(columns[7]),
                HSSH = int.Parse(columns[8]),
                ASSH = int.Parse(columns[9])
                //other int properties
            });
        }
        var dataList2 = new List<Data2>();
        foreach (var data in data2)
        {
            var columns = data.Split(';'); 
            dataList2.Add(new Data2
            {
                Home = columns[0],
                Away = columns[1],
                HODD = columns[4],
                XODD = columns[5],
                AODD = columns[6],

            });
        }
        var combinedDataList = from d1 in dataList1 
                               //join d2 in dataList2 on d1.Home equals d2.Home
                               join d2 in dataList2 on new { d1.Home, d1.Away } equals new { d2.Home, d2.Away }
                               select new CombinedData
                               {
                                   Campionato = d1.Campionato,
                                   Data = d1.Data,
                                   Home = d2.Home,
                                   Away = d2.Away,
                                   HSFT = d1.HSFT,
                                   ASFT = d1.ASFT,
                                   HSHT = d1.HSHT,
                                   ASHT = d1.ASHT,
                                   HSSH = d1.HSSH,
                                   ASSH = d1.ASSH,
                                   HODD = d2.HODD,
                                   XODD = d2.XODD,
                                   AODD = d2.AODD,
                                   RisFin = d2.RisFin,
                                   Over05SH = d2.Over05SH
                               }; //map all properties
        finabexDgv.DataSource = combinedDataList.ToList(); 
        finabexDgv.AllowUserToOrderColumns = true;   

我希望可以通过单击列标题进行排序。

我需要你的建议,请:(

星期天快乐!

单击列标题上的C#排序数据网格视图

在设计视图中,搜索"ColumnHeaderMouseClick"并双击空字段。

在新功能上,应用一些类似的代码:

dgv_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (dgv.Columns[e.ColumnIndex].SortMode != DataGridViewColumnSortMode.NotSortable)
            {
                if (e.ColumnIndex == newSortColumn )
                {
                    if (newColumnDirection == ListSortDirection.Ascending)
                        newColumnDirection = ListSortDirection.Descending;
                    else
                        newColumnDirection = ListSortDirection.Ascending;
                }
                newSortColumn = e.ColumnIndex;
                switch (newColumnDirection)
                {
                    case ListSortDirection.Ascending:
                        dgv.Sort(dgv.Columns[newSortColumn], ListSortDirection.Ascending);
                        break;
                    case ListSortDirection.Descending:
                        dgv.Sort(dgv.Columns[newSortColumn], ListSortDirection.Descending);
                        break;
                }
            }
}

在类的顶部,添加两个私有变量:

int newSortColumn;
ListSortDirection newColumnDirection = ListSortDirection.Ascending;

如果你的源代码中没有其他稀有的东西,这将起作用。

注意:这个代码不是我的,我在stackoverflow 上得到的