2012-05-13 88 views
0

NET WinForms。如何在GridView中顯示結果?

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 

我想顯示此行一個& 「」 & b在GridView控件的結果。 我應該如何更改代碼以使其正常工作?

+1

待辦事項你的意思是取這一行的值a&「」&b(其中a表示行和b - 列)? 然後像這樣使用它: Dim value As String = Me.DataGridView1.Item(b,a).Value –

+0

您可以使用泛型與DatGridView綁定 –

回答

1

我建議你的值存儲到數據表並綁定到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 
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