0

NET WinForms。

VB代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)       Handles Button1.Click

    Label1.Text = "Beginning"

    Dim a As Integer = 20
    Dim b As Integer = 3
    Do Until b > a

        a & " " & b

        a = a - 2
        b = b + 1
    Loop
    Label2.Text = "End"
End Sub

我想在 GridView中显示这一行 a & " " & b的结果。我应该如何更改代码以使其正常工作?

4

2 回答 2

1

将 DataGridView 添加到您的表单中,并添加 2 列,然后下一个更新的代码将执行此操作

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)          Handles Button1.Click

    Label1.Text = "Beginning"

    ' If the DataGridView is not bound to any data source, this code will clear content
    DataGridView1.Rows.Clear()

    Dim a As Integer = 20
    Dim b As Integer = 3
    Do Until b > a

       'a & " " & b
       ' add the row to the end of the grid with the Add() method of the Rows collection...
       DataGridView1.Rows.Add(New String(){a.ToString(), b.ToString()})

       a = a - 2
       b = b + 1
    Loop
    Label2.Text = "End"
End Sub
于 2012-05-14T00:52:30.447 回答
1

我建议您将值存储到 DataTable 并绑定到 DataGridView

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)       Handles Button1.Click

    Label1.Text = "Beginning"

    'Create a new datatable here
    Dim dt As New DataTable
    dt.Columns.Add("Result")


    Dim a As Integer = 20
    Dim b As Integer = 3
    Do Until b > a

        'Create DataRow here and put the value into DataRow
        Dim dr As DataRow = dt.NewRow
        dr("result") = a.ToString & " " & b.ToString
        'a & " " & b
        dt.Rows.Add(dr)

        a = a - 2
        b = b + 1
    Loop

    'Bind your dt into the GridView
    DataGridView.DataSource = dt

    Label2.Text = "End"

End Sub
于 2012-05-14T00:53:48.183 回答