DataGridView 使用 Structure 和 LINQ 对 txt 文件进行排序 [英] DataGridView using Structure and LINQ to order txt file

查看:24
本文介绍了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.

这篇关于DataGridView 使用 Structure 和 LINQ 对 txt 文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆