2012-05-13 3 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 

Я хочу, чтобы отобразить результат этой строки & "" & б в GridView. Как мне изменить код для правильной работы?

+1

Do вы хотите взять значение этой строки a & "" & b (где a обозначает строку и b - столбец)? Затем используйте его следующим образом: Dim value As String = Me.DataGridView1.Item (b, a) .Value –

+0

вы можете использовать дженерики для привязки к DatGridView –

ответ

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 
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 
Смежные вопросы