DataGridView 使用 Structure 和 LINQ 对 txt 文件进行排序
我的程序出现问题,我能够将所有数据拉入网格并进入正确的列/行.但是,我相信我的 LINQ 查询是错误的,它没有正确划分第 3 列并插入正确的数据.我的结果:https://gyazo.com/0f307a10dff4c015a361708ecd53c0e9,https://gyazo.com/99e9915bf4ee4e4aa3fb7f2144da1f94.我可以在代码中更改什么以将其从 0 更改为正确的百分比?感谢所有的回应!从它提取的文本文件中还有一个例子是美国运通,AXP,纽约证券交易所,消费者金融,90.73,93.04,5.56,1.01"
Having issues with my program, I'm able to get all the data to pull into the grid and into the correct columns / rows. However, my LINQ Query is wrong I believe and it's not making the 3rd column divide properly and insert the correct data. My results: https://gyazo.com/0f307a10dff4c015a361708ecd53c0e9, Correct Result: https://gyazo.com/99e9915bf4ee4e4aa3fb7f2144da1f94. What can I change in my code to change that from a 0 to the correct percentage? Appreciate all responses! Also an example from the text file it is pulling from is this "American Express,AXP,NYSE,Consumer finance,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
查看您的代码,问题并没有随着更改而消失.问题是您实际上从未为 yield
设置过值,因此它只能为零.您需要做的是为 LINQ 查询中的每个项目选择一个屈服值,例如
Looking at your code, the issue is not going away with that change. The problem is that you never actually set a value for yield
, so it can't be anything but zero. What you need to do is to select a yield value for each item in your LINQ query, e.g.
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
仅设置一次,您无需手动添加任何行.
Note that the DataSource
is set once only and you don't add any rows manually.