-1

我创建了一个程序,我使用数据表来存储一些信息,然后单击按钮,它将显示在 datagridview 中。这是我的代码......有人可以告诉我如何创建一个数组而不是这个,我研究了几个小时仍然没有帮助。

 Dim Array As New DataTable
Dim array2 As New DataTable
Dim array3 As New DataTable

Public Class Degrees

    Public Property FieldofStudy As String
    Public Property Year1986 As String
    Public Property Year2006 As String

    Public Sub New(ByVal FieldofStudy As String, ByVal Year1986 As String, ByVal Year2006 As String)
        Me.FieldofStudy = FieldofStudy
        Me.Year1986 = Year1986
        Me.Year1986 = Year2006
    End Sub
End Class

Private Sub btnChanges_Click(sender As System.Object, e As System.EventArgs) Handles btnChanges.Click

    array2.Clear()
    Dim column4 As DataColumn = New DataColumn("Field of Study")
    column4.DataType = System.Type.GetType("System.String")

    Dim column5 As DataColumn = New DataColumn("Change (1981-2006)")
    column5.DataType = System.Type.GetType("System.String")


    array2.Columns.Add(column4)
    array2.Columns.Add(column5)

    ''Display array two containing percentage changes''
    array2.Rows.Add("Computer and info. science", "214.0%")
    array2.Rows.Add("Social sciences and history", "60.7%")
    array2.Rows.Add("Business", "58.6%")
    array2.Rows.Add("Engineering", "5.3%")
    array2.Rows.Add("Education", "-0.8%")

    dgvStudies.DataSource = array2

End Sub

Private Sub btnHistogram_Click(sender As System.Object, e As System.EventArgs) Handles btnHistogram.Click

    array3.Clear()

    Dim column6 As DataColumn = New DataColumn("Field of Study")
    column6.DataType = System.Type.GetType("System.String")

    Dim column7 As DataColumn = New DataColumn("")
    column7.DataType = System.Type.GetType("System.String")

    Dim column8 As DataColumn = New DataColumn("Degrees in 2006")
    column8.DataType = System.Type.GetType("System.String")

    array3.Clear()
    array3.Columns.Add(column6)
    array3.Columns.Add(column7)
    array3.Columns.Add(column8)

    array3.Rows.Add("Computer and info. science", "*****", "47,480")
    array3.Rows.Add("Engineering", "*******", "67,045")
    array3.Rows.Add("Education", "***********", "107,238")
    array3.Rows.Add("Social sciences and history", "****************", "161,485")
    array3.Rows.Add("Business", "********************************", "67,045")

    dgvStudies.DataSource = array3

End Sub

Private Sub btnQuit_Click_1(sender As System.Object, e As System.EventArgs) Handles btnQuit.Click

    Me.Close()
End Sub

Private Sub btnDegrees_Click_1(sender As System.Object, e As System.EventArgs) Handles btnDegrees.Click

    Array.Clear()
    Dim column1 As DataColumn = New DataColumn("Field of Study")
    column1.DataType = System.Type.GetType("System.String")

    Dim column2 As DataColumn = New DataColumn("1981")
    column2.DataType = System.Type.GetType("System.String")

    Dim column3 As DataColumn = New DataColumn("2006")
    column3.DataType = System.Type.GetType("System.String")

    Array.Clear()
    Array.Columns.Add(column1)
    Array.Columns.Add(column2)
    Array.Columns.Add(column3)

    Array.Rows.Add("Business", "200,521", "318,042")
    Array.Rows.Add("Computer and info. science", "15,121", "47,480")
    Array.Rows.Add("Education", "108,074", "107,238")
    Array.Rows.Add("Engineering", "63,642", "67,045")
    Array.Rows.Add("Social sciences and history", "100,513", "161,485")

    Me.dgvStudies.DataSource = Array
End Sub

结束类

4

1 回答 1

1

虽然您的变量被命名为“array”、“array2”和“array3”,但您已将它们声明为DataTable使它们成为DataTables(而不是Arrays)的类型。DataTables 可以作为 DataGridView 的数据源正常工作。

由于您需要将它们作为数组,您可以将代码更改为以下内容:

Public Class Changes
    Public Property FieldofStudy As String = String.Empty
    Public Property Change As String = String.Empty

    Public Sub New(ByRef FieldofStudy As String, ByRef Change As String)
        Me.FieldofStudy = FieldofStudy
        Me.Change = Change
    End Sub
End Class

Public Class Histogram
    Public Property FieldofStudy As String = String.Empty
    Public Property Asterisks As String = String.Empty
    Public Property Degrees As String = String.Empty

    Public Sub New(ByRef FieldofStudy As String, ByRef Asterisks As String, ByRef Degrees As String)
        Me.FieldofStudy = FieldofStudy
        Me.Asterisks = Asterisks
        Me.Degrees = Degrees
    End Sub
End Class

Public Class Degrees
    Public Property FieldofStudy As String = String.Empty
    Public Property Year1986 As String = String.Empty
    Public Property Year2006 As String = String.Empty

    Public Sub New(ByRef FieldofStudy As String, ByRef Year1986 As String, ByRef Year2006 As String)
        Me.FieldofStudy = FieldofStudy
        Me.Year1986 = Year1986
        Me.Year2006 = Year2006
    End Sub
End Class

Private Sub btnChanges_Click(sender As System.Object, e As System.EventArgs) Handles btnChanges.Click
    Dim dtChanges As DataTable = New DataTable("Changes")
    Dim dcDiscipline As DataColumn = New DataColumn("Field of Study", GetType(String))
    Dim dcChange As DataColumn = New DataColumn("Change (1981-2006)", GetType(String))
    Dim list As ArrayList = New ArrayList
    Dim row As DataRow = Nothing
    Dim change As Changes = Nothing

    dtChanges.Columns.Add(dcDiscipline)
    dtChanges.Columns.Add(dcChange)

    'Display Degrees containing percentage changes
    dtChanges.Rows.Add("Computer and info. science", "214.0%")
    dtChanges.Rows.Add("Social sciences and history", "60.7%")
    dtChanges.Rows.Add("Business", "58.6%")
    dtChanges.Rows.Add("Engineering", "5.3%")
    dtChanges.Rows.Add("Education", "-0.8%")

    For Each row In dtChanges.Rows
        change = New Changes(row.Item(0), row.Item(1))
        list.Add(change)
    Next

    dgvStudies.DataSource = list
End Sub

Private Sub btnHistogram_Click(sender As System.Object, e As System.EventArgs) Handles btnHistogram.Click
    Dim dtHistogram As DataTable = New DataTable("Histogram")
    Dim dcDiscipline As DataColumn = New DataColumn("Field of Study", GetType(String))
    Dim dcUnnamed As DataColumn = New DataColumn("", GetType(String))
    Dim dcDegrees2006 As DataColumn = New DataColumn("Degrees in 2006", GetType(String))
    Dim list As ArrayList = New ArrayList
    Dim row As DataRow = Nothing
    Dim histo As Histogram = Nothing

    dtHistogram.Columns.Add(dcDiscipline)
    dtHistogram.Columns.Add(dcUnnamed)
    dtHistogram.Columns.Add(dcDegrees2006)

    dtHistogram.Rows.Add("Computer and info. science", "*****", "47,480")
    dtHistogram.Rows.Add("Engineering", "*******", "67,045")
    dtHistogram.Rows.Add("Education", "***********", "107,238")
    dtHistogram.Rows.Add("Social sciences and history", "****************", "161,485")
    dtHistogram.Rows.Add("Business", "********************************", "67,045")

    For Each row In dtHistogram.Rows
        histo = New Histogram(row.Item(0), row.Item(1), row.Item(2))
        list.Add(histo)
    Next

    dgvStudies.DataSource = list
End Sub

Private Sub btnDegrees_Click(sender As System.Object, e As System.EventArgs) Handles btnDegrees.Click
    Dim dtDegrees As DataTable = New DataTable("Degrees")
    Dim dcDiscipline As DataColumn = New DataColumn("Field of Study", GetType(String))
    Dim dcYear1986 As DataColumn = New DataColumn("1981", GetType(String))
    Dim dcYear2006 As DataColumn = New DataColumn("2006", GetType(String))
    Dim list As ArrayList = New ArrayList
    Dim row As DataRow = Nothing
    Dim degree As Degrees = Nothing

    dtDegrees.Columns.Add(dcDiscipline)
    dtDegrees.Columns.Add(dcYear1986)
    dtDegrees.Columns.Add(dcYear2006)

    dtDegrees.Rows.Add("Business", "200,521", "318,042")
    dtDegrees.Rows.Add("Computer and info. science", "15,121", "47,480")
    dtDegrees.Rows.Add("Education", "108,074", "107,238")
    dtDegrees.Rows.Add("Engineering", "63,642", "67,045")
    dtDegrees.Rows.Add("Social sciences and history", "100,513", "161,485")

    For Each row In dtDegrees.Rows
        degree = New Degrees(row.Item(0), row.Item(1), row.Item(2))
        list.Add(degree)
    Next

    dgvStudies.DataSource = list
End Sub

您可以通过完全不使用 DataTables 来进一步简化这一点:

Public Class Changes
    Public Property FieldofStudy As String = String.Empty
    Public Property Change As String = String.Empty

    Public Sub New(ByRef FieldofStudy As String, ByRef Change As String)
        Me.FieldofStudy = FieldofStudy
        Me.Change = Change
    End Sub
End Class

Public Class Histogram
    Public Property FieldofStudy As String = String.Empty
    Public Property Asterisks As String = String.Empty
    Public Property Degrees As String = String.Empty

    Public Sub New(ByRef FieldofStudy As String, ByRef Asterisks As String, ByRef Degrees As String)
        Me.FieldofStudy = FieldofStudy
        Me.Asterisks = Asterisks
        Me.Degrees = Degrees
    End Sub
End Class

Public Class Degrees
    Public Property FieldofStudy As String = String.Empty
    Public Property Year1986 As String = String.Empty
    Public Property Year2006 As String = String.Empty

    Public Sub New(ByRef FieldofStudy As String, ByRef Year1986 As String, ByRef Year2006 As String)
        Me.FieldofStudy = FieldofStudy
        Me.Year1986 = Year1986
        Me.Year2006 = Year2006
    End Sub
End Class

Private Sub btnChanges_Click(sender As System.Object, e As System.EventArgs) Handles btnChanges.Click
    Dim list As ArrayList = New ArrayList

    'Display Degrees containing percentage changes
    list.Add(New Changes("Computer and info. science", "214.0%"))
    list.Add(New Changes("Social sciences and history", "60.7%"))
    list.Add(New Changes("Business", "58.6%"))
    list.Add(New Changes("Engineering", "5.3%"))
    list.Add(New Changes("Education", "-0.8%"))

    dgvStudies.DataSource = list
End Sub

Private Sub btnHistogram_Click(sender As System.Object, e As System.EventArgs) Handles btnHistogram.Click
    Dim list As ArrayList = New ArrayList

    list.Add(New Histogram("Computer and info. science", "*****", "47,480"))
    list.Add(New Histogram("Engineering", "*******", "67,045"))
    list.Add(New Histogram("Education", "***********", "107,238"))
    list.Add(New Histogram("Social sciences and history", "****************", "161,485"))
    list.Add(New Histogram("Business", "********************************", "67,045"))

    dgvStudies.DataSource = list
End Sub

Private Sub btnDegrees_Click(sender As System.Object, e As System.EventArgs) Handles btnDegrees.Click
    Dim list As ArrayList = New ArrayList

    list.Add(New Degrees("Business", "200,521", "318,042"))
    list.Add(New Degrees("Computer and info. science", "15,121", "47,480"))
    list.Add(New Degrees("Education", "108,074", "107,238"))
    list.Add(New Degrees("Engineering", "63,642", "67,045"))
    list.Add(New Degrees("Social sciences and history", "100,513", "161,485"))

    dgvStudies.DataSource = list
End Sub
于 2012-10-06T16:31:36.107 回答