简体   繁体   中英

DataGridView ComboBox

I have a program which has a datagridview with two combobox and two textbox. The first combobox contains OIDs and the other Combobox contains SNMP Operation (Get & GetNext). Now I want to add another ComboBox beside my OID's Combobox which contains description for the OIDs (like 1.3.6.1.2.1.1.1.0 for sysDescr and so on), and also the value of the New ComboBox(the one with description of OIDs) changes automatically as the user changes the OID in the first combobox. Is it possible? If yes then how? Here is the code: Imports System Imports SnmpSharpNet

Public Class Form1 Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    DataGridView1.Columns.Add("NameOne", "Column One")
    DataGridView1.Columns.Add("NameTwo", "Column Two")

    Dim dataGridRow As New DataGridViewRow()
    Dim cells As DataGridViewCell() = New DataGridViewCell(1) {}
    Dim txt1A As New DataGridViewTextBoxCell()
    Dim txt1B As New DataGridViewTextBoxCell()
    txt1A.Value = "Host"
    txt1B.Value = "115.186.118.130"
    dataGridRow.Cells.Add(txt1A)
    txt1A.[ReadOnly] = True
    dataGridRow.Cells.Add(txt1B)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)

    dataGridRow = New DataGridViewRow()
    cells = New DataGridViewCell(1) {}

    Dim txt2A As New DataGridViewTextBoxCell()
    Dim cbo1 As New DataGridViewComboBoxCell()

    cbo1.Items.Add("1.3.6.1.2.1.1.1.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.2.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.3.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.4.0")
    cbo1.Items.Add("1.3.6.1.2.1.1.5.0")

    cbo1.Value = cbo1.Items(0)

    cbo1.[ReadOnly] = False
    txt2A.Value = "OIDs"
    dataGridRow.Cells.Add(txt2A)
    txt2A.[ReadOnly] = True
    dataGridRow.Cells.Add(cbo1)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)

    Dim requestOid() As String
    requestOid = New String() {cbo1.Selected}

    dataGridRow = New DataGridViewRow()
    cells = New DataGridViewCell(1) {}
    Dim txt3A As New DataGridViewTextBoxCell()
    Dim cbo2 As New DataGridViewComboBoxCell()

    cbo2.Items.Add("Get")
    cbo2.Items.Add("GetNext")
    cbo2.Value = cbo2.Items(0)

    cbo2.[ReadOnly] = False
    txt3A.Value = "SNMP Operation"
    dataGridRow.Cells.Add(txt3A)
    txt3A.[ReadOnly] = True
    dataGridRow.Cells.Add(cbo2)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)

    dataGridRow = New DataGridViewRow()
    cells = New DataGridViewCell(1) {}
    Dim txt4A As New DataGridViewTextBoxCell()
    Dim txt4B As New DataGridViewTextBoxCell()
    txt4A.Value = "Community String"
    txt4B.Value = "public"
    dataGridRow.Cells.Add(txt4A)
    dataGridRow.Cells.Add(txt4B)
    dataGridRow.Height = 20
    DataGridView1.Rows.Add(dataGridRow)
End Sub

Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)
    Dim comboControl As DataGridViewComboBoxEditingControl = TryCast(e.Control, DataGridViewComboBoxEditingControl)
    If comboControl IsNot Nothing Then
        ' Set the DropDown style to get an editable ComboBox
        If comboControl.DropDownStyle <> ComboBoxStyle.DropDown Then
            comboControl.DropDownStyle = ComboBoxStyle.DropDown
        End If
    End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim result As Dictionary(Of Oid, AsnType)

    Dim cbo1 As New DataGridViewComboBoxCell()


    Dim txt1B As New DataGridViewTextBoxCell()
    txt1B.Value = "203.81.211.117"

    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 snmp As New SimpleSnmp
    snmp = New SimpleSnmp(DataGridView1.Rows(0).Cells(1).Value.ToString, DataGridView1.Rows(3).Cells(1).Value.ToString)

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

    'result = snmp.GetNext(SnmpVersion.Ver1, requestoid)
    'If (txtsnmpaction = "GetBulk")
    'result = snmp.GetBulk(New String() {".1.3.6.1.2", ".1.3.6.1.3"})
    ' End If
    If Not snmp.Valid Then

        MessageBox.Show("Invalid hostname/community")

    End If
    If result IsNot Nothing Then
        Dim kvp As KeyValuePair(Of Oid, AsnType)
        For Each kvp In result
            MessageBox.Show(kvp.Key.ToString)
            MessageBox.Show(SnmpConstants.GetTypeName(kvp.Value.Type))
            MessageBox.Show(kvp.Value.ToString())

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

End Sub

End Class

I'd make the SNMP Operation a read-only text field and set it programatically. Handle the DataGridView1.CellValueChanged event.

EDIT: Changing description rather than SNMP operation.

Private Sub DataGridView1_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    If e.ColumnIndex = 3 Then
        Dim Row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
        Dim Description As String = Nothing
        Select Case Row.Cells(3).Value
            Case "1.3.6.1.2.1.1.1.0" : Description = "sysDescr"
            ...
        End Select
        Row.Cells(5).Value = Description ' Change (5) to description column index.
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM