使用数组而不是数据表在vb中编写程序

时间:2012-10-04 17:09:45

标签: vb.net

我的代码显示有关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

1 个答案:

答案 0 :(得分:1)

如果要使用数组而不是DataTable,还需要创建与DataTable结构对应的对象。这是使用数组和Degrees对象的“度数实现”的示例。

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