读取两个文本文件并加入datagridview中的列c#

时间:2015-09-14 09:03:17

标签: c# datagridview text-files

我有两个这样的文本文件:

文字档案1:

Campionato;Data;Home;Away;HSFT;ASFT;HSHT;HSHT
DenDiv1;07.09.2015;Silkeborg;Helsingor;1;0;0;0
DenDiv1;06.09.2015;Naestved;Roskilde;1;1;0;0
DenDiv1;06.09.2015;Lyngby;Vejle;3;2;2;0

文字档案2:

Data;Home;Away;HODD;AODD
07.09.2015;Silkeborg;Helsingor;1.50;4.09;5.71
06.09.2015;Naestved;Roskilde;2.54;3.20;2.63
06.09.2015;Lyngby;Vejle;2.12;3.33;3.20

我需要加入并在datagridview中显示它们。请问你能帮帮我吗?谢谢! :)

更新:Home和Away列都是我的主键。

1 个答案:

答案 0 :(得分:0)

这是数据类:

public class MyData
{
    public String Campionato { get; set; }
    public DateTime 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 Hsht_2 { get; set; }
    public float Hodd { get; set; }
    public float Aodd { get; set; }
    public float Unknown { get; set; }
}

这里是读取文件和加入数据的代码:

var file1Data = File.ReadLines(@"File1.csv")
                    .Skip(1) // Skip header
                    .Select(line => line.Split(';'))
                    .Select(elements => new MyData
                    {
                        Campionato = elements[0],
                        Data = DateTime.ParseExact(elements[1], "MM'.'dd'.'yyyy", CultureInfo.InvariantCulture),
                        Home = elements[2],
                        Away = elements[3],
                        Hsft = Int32.Parse(elements[4]),
                        Asft = Int32.Parse(elements[5]),
                        Hsht = Int32.Parse(elements[6]),
                        Hsht_2 = Int32.Parse(elements[7])
                    });

var file2Data = File.ReadLines(@"File2.csv")
                    .Skip(1) // Skip header
                    .Select(line => line.Split(';'))
                    .Select(elements => new MyData
                    {
                        Data = DateTime.ParseExact(elements[0], "MM'.'dd'.'yyyy", CultureInfo.InvariantCulture),
                        Home = elements[1],
                        Away = elements[2],
                        Hodd = float.Parse(elements[3]),
                        Aodd = float.Parse(elements[4])
                    });

var joinedData = file1Data.Join(
                        file2Data,
                        // Key generation should be optimized. Maybe take a look at http://stackoverflow.com/q/263400/1838048
                        myData => myData.Data.GetHashCode() + myData.Home.GetHashCode() + myData.Away.GetHashCode(),
                        myData => myData.Data.GetHashCode() + myData.Home.GetHashCode() + myData.Away.GetHashCode(),
                        (file1, file2) => new MyData
                        {
                            Campionato = file1.Campionato,
                            Data = file1.Data,
                            Home = file1.Home,
                            Away = file1.Away,
                            Hsft = file1.Hsft,
                            Asft = file1.Asft,
                            Hsht = file1.Hsht,
                            Hsht_2 = file1.Hsht_2,
                            Hodd = file2.Hodd,
                            Aodd = file2.Aodd,
                            Unknown = file2.Unknown
                        });


myDataGridView.DataSource = joinedData.ToList();

有很多硬编码的东西,没有任何错误检查。所以你自己有很多改进,但它应该给你一个很好的起点。