1

My pseudocode is the following -Select a textfile through a button and collect information. -divide the data; Let data = line.Split(","c) -Move the data to the appropriate area on the program -The data would look like; Bugs Bunny,VB101,Fall 2013,bugs.jpg,85,100,80,92,70,95,88,92 -Im trying to put the Name, Class, Semester into a label, the bugs.jpg into a picturebox, and then put the rest of the info(testScores) into a DataGrid. Is it possible to grab these values and split them up to different areas of the program?

This is my code as of now; I know it needs lots of work but this is where I'm currently at.

Public Class Form1
    Dim student As String
    Dim subject As String
    Dim semester As String
    Dim image As Image


    Private Sub btnSelect_Click(sender As System.Object, e As System.EventArgs) Handles btnSelect.Click
        Dim textFile As String
        OpenFileDialog1.ShowDialog() 'Open dialog box appears and program pauses until a text file is selected
        textFile = OpenFileDialog1.FileName


        'Dim student() As String = IO.File.ReadAllLines(textFile)
        Dim query = From line In IO.File.ReadAllLines(textFile)
                    Let data = line.Split(","c)
                    Let student = data(0)
                    Let subject = data(1)
                    Let semester = data(2)
                    Let image = data(3)
                    Let p1 = data(4)
                    Let p2 = data(5)
                    Let p3 = data(6)
                    Let p4 = data(7)
                    Let p5 = data(8)
                    Let p6 = data(9)
                    Let exam1 = data(10)
                    Let exam2 = data(11)



        dgvOutput.DataSource = query.ToList
        dgvOutput.CurrentCell = Nothing
        dgvOutput.Columns("P1").HeaderText = "P1"
        dgvOutput.Columns("P2").HeaderText = "P2"
        dgvOutput.Columns("P3").HeaderText = "P3"
        dgvOutput.Columns("P4").HeaderText = "P4"
        dgvOutput.Columns("P5").HeaderText = "P5"
        dgvOutput.Columns("P6").HeaderText = "P6"
        dgvOutput.Columns("exam1").HeaderText = "exam1"
        dgvOutput.Columns("exam2").HeaderText = "exam2"


    End Sub



End Class
4

2 回答 2

0

DGV 将使用此实现自动命名列。为数据使用一个类,然后为文件中找到的所有学生使用一个集合。

Private Students As New List(Of Student)

'in a sub routine that collects the data
Using sr As New StreamReader({path})
  While Not sr.EndOfStream
    Dim student As New Student
    Dim data = line.Split(","c)
    student.name = data(0)
    student.subject = data(1)
    student.semester = data(2)
    student.image = data(3)
    student.p1 = data(4)
    student.p2 = data(5)
    student.p3 = data(6)
    student.p4 = data(7)
    student.p5 = data(8)
    student.p6 = data(9)
    student.exam1 = data(10)
    student.exam2 = data(11)
    Students.Add(student)
  End While
End Using
dgvOutput.DataSource = Students

自定义类:

Public Class Student
    Public Property name As String
    Public Property subject As String
    Public Property semester As String
    Public Property image As String
    Public Property p1 As String
    Public Property p2 As String
    Public Property p3 As String
    Public Property p4 As String
    Public Property p5 As String
    Public Property p6 As String
    Public Property exam1 As String
    Public Property exam2 As String
End Class
于 2013-11-14T01:21:02.503 回答
0

或者,您应该使用 DataTable 将数据绑定到 DataGrid

Dim table = New DataTable()
table.Columns.Add("P1")
table.Columns.Add("P2")
table.Columns.Add("P3")
table.Columns.Add("P4")
table.Columns.Add("P5")
table.Columns.Add("P6")
table.Columns.Add("exam1")
table.Columns.Add("exam2")

Dim row = table.NewRow()
row.Item("P1") = P1
row.Item("P2") = P2
row.Item("P3") = P3
row.Item("P4") = P4
row.Item("P5") = P5
row.Item("P5") = P6
table.Rows.Add(row)

dgvOutput.DataSource = table
于 2013-11-14T01:48:30.187 回答