在DataGridView中获取下一行中的结果

时间:2011-07-20 20:25:37

标签: vb.net datagridview

我在DataGridView中有一行显示所选Oids的不同结果。问题是下一个结果替换了同一行中的第一个..是否有任何方法可以在下一行中获得下一个结果,以便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

1 个答案:

答案 0 :(得分: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)

这将使用这些值创建一个新行,而不是覆盖现有值。