2
votes

:) I have a datagridview and I fill it by List (I get data from 2 text files), but when I tried to click on a column header (I tried with all columns headers), I can't sort my datagridview data. This is my code:

 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;   

I'd like to have a possibility to sort by clicking columns headers.

I need your advices, please :)

Happy Sunday!

1
did you check sortmode property? afaik by default it should behave like you want to...?! - Falco Alexander
There is a OnColumnHeaderMouseClick event link Create field for your datasource-list, methods for sorting it depend on header was clicked. Subscribe to OnColumnHeaderMouseClick event, find out which header was clicked, sort your datasource, and reconnect to it if necessary. Try this straight decision, if will not find any better. - Oxoron
Sorry but i'm a newbie and I don't understand your solution... Can you give me some examples,. please? Thanks :) - Moncicci90
check for each column it the SortMode is on Automatic. If not, than put them all on Automatic, if yes then show the code for each event of the datagridview. - GuidoG
I tried but nothing. I read that when datagridview's source is a list, there is just a special way to sort it here: stackoverflow.com/questions/8013439/… But i don't know exactly how I can implement it in my code. Thanks for your answer :) - Moncicci90

1 Answers

0
votes

On your desing view, search the "ColumnHeaderMouseClick" and double click on the empty field.

on the new function, apply some code like this:

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;
                }
            }
}

and at the top of the class, add two private variables:

int newSortColumn;
ListSortDirection newColumnDirection = ListSortDirection.Ascending;

This will work if you don't have anything else rare at your source code.

Note: This code is not mine, i got it form here at stackoverflow