DataGridView使用Structure和LINQ命令txt文件

时间:2016-11-22 02:58:44

标签: vb.net

我的程序出现问题,我能够将所有数据拉入网格并进入正确的列/行。但是,我认为我的LINQ查询是错误的,它并没有使第3列正确划分并插入正确的数据。我的结果:https://gyazo.com/0f307a10dff4c015a361708ecd53c0e9,正确结果:https://gyazo.com/99e9915bf4ee4e4aa3fb7f2144da1f94。我可以在代码中更改哪些内容以将其从0更改为正确的百分比?感谢所有回复!它所提供的文本文件的一个例子就是这个"美国运通,AXP,纽约证券交易所,消费者金融,90.73,93.04,5.56,1.01"

    Public Class frmDow

Structure stock
    Dim company As String
    Dim symbol As String
    Dim exchange As String
    Dim industry As String
    Dim price2013 As Double
    Dim price2014 As Double
    Dim earningsPerShare As Double
    Dim dividend As Double
End Structure

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click

    Dim inputData() As String = IO.File.ReadAllLines("DOW2014.txt")
    Dim stockData(29) As stock
    Dim line, data() As String
    Dim yield As Double

    For i As Integer = 0 To (inputData.Length - 1)
        line = inputData(i)
        data = line.Split(","c)
        stockData(i).company = data(0)
        stockData(i).symbol = data(1)
        stockData(i).price2014 = data(5)
        stockData(i).dividend = data(7)
    Next

    Dim stockQuery = From stock In stockData
                     Where yield = data(5) / data(7)
                     Order By yield Descending
                     Select stock

    For Each stockName In stockQuery
        dgvResults.DataSource = stockData
    Next

    For Each s As stock In stockData
        dgvResults.Rows.Add(s.company, s.symbol, yield)
    Next

End Sub

End Class

1 个答案:

答案 0 :(得分:0)

查看您的代码,问题不会消失。问题是你实际上从未为yield设置一个值,所以它不能是零。您需要做的是为LINQ查询中的每个项目选择一个屈服值,例如

Dim stockQuery = From stock In stockData
                 Where yield = data(5) / data(7)
                 Order By yield Descending
                 Select New With {.Company = stock.company,
                                  .Symbol = stock.Symbol,
                                  .Yield = stock.price2014 / stock.dividend}

dgvResults.DataSource = stockQuery.ToList()

请注意,DataSource仅设置一次,您不会手动添加任何行。