0

I'm having issues with my DataGridView not displaying the proper results, below I'll post my code, some of it is commented out because I don't want it to sort in descending order yet because I can't even get it to display properly. What am I missing? When I click my button, the grid creates the correct number of boxes for the results, however they are all empty. An example of one line from the text file is something such as "American Express,AXP,NYSE,Consumer finance,90.73,93.04,5.56,1.01" I also want to note that this is my first time using structures and trying to understand it, so let me know if I'm doing anything wrong, thanks!

    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(240) 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 = CDbl(data(5))
        stockData(i).dividend = CDbl(data(7))
    Next

    dgvResults.DataSource = stockData

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

End Sub
End Class
Devin
  • 277
  • 2
  • 15
  • Databinding works with properties - you just have some fields. That should also be a class rather than a structure. [Choosing Between Class and Struct](https://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx) If you turn it into a List containing class object, you could just serialize it rather than reading and parsing it – Ňɏssa Pøngjǣrdenlarp Nov 22 '16 at 01:52
  • meaning it should be `Class stock : Property company As String` etc., but OleDb should be easier http://stackoverflow.com/questions/6813607/parsing-csv-using-oledb-using-c-sharp – Slai Nov 22 '16 at 01:57
  • For this problem I'm required to use a structure. So I don't think I can use a Class, correct? Not here to look to be given answers, just want some help on my currently worked out code and why it's not working the way I need it to. The gyazo is the example of what I need this program to do. https://gyazo.com/c6067ff4fdb55d09fcb42a29ac2e97da – Devin Nov 22 '16 at 02:00
  • Assuming you've already added columns to your grid, you could do something like `For Each s As stock In stockData dgvResults.Rows.Add(s.company, s.symbol, s.price2014, s.dividend) Next` – Jim Hewitt Nov 22 '16 at 02:15
  • 1
    Yeah I've added the columns, the correct number of columns and rows show up, they are all empty though. That works though thanks! Now to get the LINQ part to work properly. – Devin Nov 22 '16 at 02:17
  • As we said, it's not working because there are no properties. Seems like they will have to come from the LINQ query. For Example `dgvResults.DataSource = (From s In stockData Select New With {.Company = s.company}).ToList` – Slai Nov 22 '16 at 02:20
  • Do not use structure for data models. Anything more than 16 bytes - use class – T.S. Nov 22 '16 at 03:49
  • 1
    Updated thread http://stackoverflow.com/questions/40733091/datagridview-using-structure-and-linq-to-order-txt-file – Devin Nov 22 '16 at 04:03

0 Answers0