:) 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!
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.