0

我有在 datagridview 中显示有关度数信息的代码。该程序有效,但我刚刚发现我需要使用数组显示此信息。一旦我弄清楚如何在数据表中显示它,这真的很容易,但我需要用另一种方式来做。请给点建议!

Dim array As New DataTable
Dim array2 As New DataTable
Dim array3 As New DataTable


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

    Me.Close()

End Sub

Private Sub btnDegrees_Click(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

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
End Class
4

1 回答 1

1

如果您想使用数组而不是 a,DataTable您还需要创建一个与您的 DataTable 结构相对应的对象。这是使用数组和Degrees对象的“Degrees implementation”示例。

Sub Main
    dim arr = new Degrees() { new Degrees("Business", "200,521", "318,042"),  new Degrees("Computer and info. science", "15,121", "47,480")}
    Me.dgvStudies.DataSource = arr
End Sub

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.Year2006 = year2006
    end sub
end class
于 2012-10-04T17:48:51.637 回答