2011-07-20 49 views
2

我在DataGridView中有一個Row,它顯示了所選Oid的不同結果..問題是下一個結果會替換同一行中的第一個結果..是否有任何方法在下一行中獲取下一個結果,以便DataGridview也可以顯示以前的結果..我的datagrid沒有綁定到任何數據源。在DataGridView中的下一行獲取結果

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


    Dim host As String 
    Dim community As String 
    host = DataGridView1.Rows(0).Cells(1).Value.ToString 
    community = DataGridView1.Rows(3).Cells(1).Value.ToString 
    Dim txt4B As New DataGridViewTextBoxCell() 
    txt4B.Value = "public" 



    Dim result As Dictionary(Of Oid, AsnType) 

    Dim requestOid() As String = Nothing 

    Dim snmp As New SimpleSnmp 
    snmp = New SimpleSnmp(DataGridView1.Rows(0).Cells(1).Value.ToString, DataGridView1.Rows(3).Cells(1).Value.ToString) 

    result = snmp.Get(SnmpVersion.Ver1, New String() {DataGridView1.Rows(1).Cells(1).Value.ToString()}) 


    If Not snmp.Valid Then 

     MessageBox.Show("Invalid hostname/community") 

    End If 

    If result IsNot Nothing Then 
     For Each kvp In result 
      DataGridView2.Rows(0).Cells(0).Value = SnmpConstants.GetTypeName(kvp.Value.Type) 
      DataGridView2.Rows(0).Cells(1).Value = kvp.Key.ToString 
      DataGridView2.Rows(0).Cells(2).Value = kvp.Value.ToString() 


     Next kvp 
    Else 
     MessageBox.Show("No results received") 
    End If 

    DataGridView2.AutoResizeColumn(0) 
    DataGridView2.AutoResizeColumn(1) 
    DataGridView2.AutoResizeColumn(2) 


End Sub 

回答

0

替換...

 DataGridView2.Rows(0).Cells(0).Value = SnmpConstants.GetTypeName(kvp.Value.Type) 
     DataGridView2.Rows(0).Cells(1).Value = kvp.Key.ToString 
     DataGridView2.Rows(0).Cells(2).Value = kvp.Value.ToString() 

像這樣的東西......

Dim NewRow As New DataGridViewRow 

    Dim Cell1 As DataGridViewCell 

    Cell1.Value = SnmpConstants.GetTypeName(kvp.Value.Type) 

    Dim Cell2 As DataGridViewCell 

    Cell2.Value = kvp.Key.ToString() 

    Dim Cell3 As DataGridViewCell 

    Cell3.Value = kvp.Value.ToString() 

    NewRow.Cells.Add(Cell1) 
    NewRow.Cells.Add(Cell2) 
    NewRow.Cells.Add(Cell3) 

    DataGridView2.Rows.Add(NewRow) 

這將創建一個新的行與價值觀,而不是覆蓋現有值。