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
Related
I have been searching for days, for any possible reference or suggestions and everything I've come across hasn't worked.
The goal:
User will select options in ComboBox1 that will then determine the available options in ComboBox2, then will populate a list of operations in ListBox1.
When the user selects available operations in ListBox1, I need the output to be the sum of values (total time in minutes in this case) into a label for display.
The data used in stored in a local db. So far everything works with my comboboxes and the listbox.
Im attempting to get the Text value, of all selected items, in ListBox1 to output the numeric value in my table (column 4 "OperationsTime"), into a label that will display the sum of all the selections (Total Time In Minutes).
Some Things I have Tried From Other Posts:
Label9.Text = ListBox1.ValueMember
Label9.Text = ListBox1.ValueMember.ToString
Label9.Text = CType(ListBox1.SelectedItem, DataRowView).Row.Item("OperationsTime").ToString
Attempted using Double:
Dim Total As Double = 0
For Each Time As Integer In ListBox1.SelectedItems
Total += CDbl(Time.ToString.Substring(Time.ToString.LastIndexOf(",") + 1))
Next
Label9.Text = Total.ToString
Screen Shot of the Table:
Operations Data Table
Below is my code:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Public Class MainHome
Private Function GetData(ByVal sql As String) As DataTable
Dim constr As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\hartj\Documents\visual studio 2015\Projects\TIMEMATRIX2.0\TIMEMATRIX2.0\TMX.mdf;Integrated Security=True"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Dim row As DataRow = dt.NewRow()
row(0) = 1
row(1) = "Please Select"
dt.Rows.InsertAt(row, 0)
Return dt
End Using
End Using
End Function
Private Sub MainHome_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = Me.GetData("SELECT SizeId, SizeName FROM Size")
ComboBox1.DisplayMember = "SizeName"
ComboBox1.ValueMember = "SizeId"
ComboBox2.Enabled = False
ComboBox3.Enabled = False
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
ComboBox2.DataSource = Nothing
ComboBox3.DataSource = Nothing
ComboBox2.Enabled = False
ComboBox3.Enabled = False
If ComboBox1.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT DetailLevelId, DetailLevelName FROM DetailLevel WHERE SizeId = {0}", ComboBox1.SelectedValue)
ComboBox2.DataSource = Me.GetData(sql)
ComboBox2.DisplayMember = "DetailLevelName"
ComboBox2.ValueMember = "DetailLevelId"
ComboBox2.Enabled = True
End If
End Sub
Private Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
ListBox1.DataSource = Nothing
ListBox1.Enabled = False
If ComboBox2.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT OperationsId, OperationsName, OperationsTime FROM Operations WHERE DetailLevelId = {0}", ComboBox2.SelectedValue)
ListBox1.DataSource = Me.GetData(sql)
ListBox1.ValueMember = "OperationsName"
ListBox1.ValueMember = "OperationsTime"
ListBox1.Enabled = True
Label9.Text = CType(ListBox1.SelectedValue, Integer).ToString
'Label.Text = CType(cbbank.SelectedItem, DataRowView).Row.Item("Account").ToString
End IF
End Sub
Dim totalOperationsTime As Double
For Each view As DataRowView In ListBox1.SelectedItems
totalOperationsTime += CDbl(view("OperationsTime"))
Next
There's no need to get the DataRow from the DataRowView because you can access the field values directly from the DataRowView. It can and does do many of the same things that the DataRow does.
That's the most conventional way but there are other options too. You could throw some LINQ at it:
Dim totalOperationsTime = ListBox1.SelectedItems.Cast(Of DataRowView)().
Sum(Function(view) CDbl(view("OperationsTime")))
It is somewhat annoying that the ValueMember property only helps you get a value for the SelectedItem. Here's a class I wrote some time ago that adds a GetItemValue method that makes use of the ValueMember much as the GetItemText method does for the DisplayMember:
Public Class ListBoxEx
Inherits ListBox
Public Function GetItemValue(item As Object) As Object
Dim index = Me.Items.IndexOf(item)
If (index <> -1 AndAlso Me.DataManager IsNot Nothing) Then
Return Me.FilterItemOnProperty(Me.DataManager.List(index), Me.ValueMember)
End If
Return Nothing
End Function
End Class
If you use that control instead of a regular ListBox then you can do this:
Dim totalOperationsTime As Double
For Each item In ListBoxEx1.SelectedItems
totalOperationsTime += CDbl(ListBoxEx1.GetItemValue(item))
Next
or this:
Dim totalOperationsTime = ListBox1.SelectedItems.Cast(Of Object)().
Sum(Function(item) CDbl(ListBoxEx1.GetItemValue(item)))
One advantage of using that custom control is that you don't have to know what type the data source or its items are. You only have to know that the ValueMember has been set.
I made a few changes to your code. It works with a ListBox. See comments for details.
' "Please Select" doesn't work well in the ListBox, I added it as an option
Private Shared Function GetData(ByVal sql As String, insertPleaseSelect As Boolean) As DataTable
Dim constr As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\hartj\Documents\visual studio 2015\Projects\TIMEMATRIX2.0\TIMEMATRIX2.0\TMX.mdf;Integrated Security=True"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
If insertPleaseSelect Then
Dim row As DataRow = dt.NewRow()
row(0) = 1
row(1) = "Please Select"
dt.Rows.InsertAt(row, 0)
End If
Return dt
End Using
End Using
End Function
Private Sub MainHome_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = GetData("SELECT [SizeId], [SizeName] FROM [Size]", True)
ComboBox1.DisplayMember = "SizeName"
ComboBox1.ValueMember = "SizeId"
ComboBox2.Enabled = False
ListBox1.SelectionMode = SelectionMode.MultiSimple ' allow multi-select
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
ComboBox2.DataSource = Nothing
ComboBox2.Enabled = False
If ComboBox1.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT [DetailLevelId], [DetailLevelName] FROM [DetailLevel] WHERE [SizeId] = {0}", ComboBox1.SelectedValue)
ComboBox2.DataSource = GetData(sql, True)
ComboBox2.DisplayMember = "DetailLevelName"
ComboBox2.ValueMember = "DetailLevelId"
ComboBox2.Enabled = True
End If
End Sub
Private Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
ListBox1.DataSource = Nothing
ListBox1.Enabled = False
If ComboBox2.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT [OperationsId], [OperationsName], [OperationsTime] FROM [Operations] WHERE [DetailLevelId] = {0}", ComboBox2.SelectedValue)
ListBox1.DataSource = GetData(sql, False)
ListBox1.DisplayMember = "OperationsName" ' changed this from ValueMember to DisplayMember
ListBox1.ValueMember = "OperationsTime"
ListBox1.Enabled = True
ListBox1.ClearSelected() ' Every time the ListBox is populated, clear it
End If
End Sub
' Added this handler to respond to user input, not programmatic selection changes
Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click
' Here is the sum
Label9.Text = ListBox1.SelectedItems.OfType(Of DataRowView).Sum(Function(o) CType(o("OperationsTime"), Double))
End Sub
I am getting Object reference not set to an instance of an object error on chart creation with threads, It was running fine without threads but I have to do it with threads, please help
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
trd = New Thread(AddressOf ThreadTask)
trd.IsBackground = True
trd.Start()
End sub
Function
Private Sub ThreadTask()
Dim Data As String() = Functions.runAnalyticsReportSample(100278)
Functions.CredHolder(Session("User"), Session("Pw"))
Dim split As String() = {""}
Dim table As New DataTable()
Dim colB As DataColumn = table.Columns.Add("A", GetType(String))
Dim colD As DataColumn = table.Columns.Add("B", GetType(String))
For Each line In Data
split = line.Split(","c)
Dim row As DataRow = table.NewRow()
row.SetField(colB, split(0))
row.SetField(colD, split(1))
table.Rows.Add(row)
Next
'Chart1.Series.Add("test")
Chart1.Series("Series1").XValueMember = "A"
Chart1.Series("Series1").YValueMembers = "B"
Chart1.Series("Series1").IsValueShownAsLabel = True
Chart1.Series("Series1").IsVisibleInLegend = False
Chart1.DataSource = table
Chart1.DataBind()
End Sub
I have a list box bound to a list is a class. All works fine until I try to add a new item to the list. during this process the data source is set to nothing to refresh the list and apparently 'Refresh' doesn't do it. The list gets refreshed and the other controls bound to the listboxes data show that the list is there and is correct however the list appears empty although it does show scroll bars. I have tried to change the font color, just in case.. Nothing!
Does someone know why this happens? how to fix it? Or a better way to refresh?
Code:
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
'lbNames is the listbox carrying all the data
Dim oContacts As List(Of clsContact) = lbNames.DataSource
lbNames.DataSource = Nothing
'Build the new Item, add it to the collection
Dim oNewCont As New clsContact
oNewCont.Editable = True
oNewCont.IsActive = True
oNewCont.Firstname = "Jimmy"
oNewCont.Lastname = "Smith"
oContacts.Add(oNewCont)
lbNames.Refresh()
' Re-Set up Autocomplete text box
Dim MySource As New AutoCompleteStringCollection()
For Each oc As clsContact In oContacts
MySource.Add(oc.FullName)
Next
txtName.AutoCompleteMode = AutoCompleteMode.Suggest
txtName.AutoCompleteCustomSource = MySource
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource
'Set List Box data back to the collection
lbNames.DataSource = oContacts
lbNames.DisplayMember = "FullName"
End Sub
The starting LOAD:
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim oCont As List(Of clsContact)
lbNames.DrawMode = DrawMode.OwnerDrawVariable
Dim oTypes As List(Of clsPhoneType) = loadTypes()
cboPhoneType.DataSource = oTypes
cboPhoneType.DisplayMember = "Type"
cboPhoneType.ValueMember = "ID"
oCont = LoadNames()
lbNames.DataSource = oCont
lbNames.DisplayMember = "FullName"
Dim MySource As New AutoCompleteStringCollection()
For Each oc As clsContact In oCont
MySource.Add(oc.FullName)
Next
txtName.AutoCompleteMode = AutoCompleteMode.Suggest
txtName.AutoCompleteCustomSource = MySource
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource
End Sub
Private Sub lbNames_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles lbNames.DrawItem
e.DrawBackground()
Dim textBrush As Brush = Brushes.Black
Dim drawFont As System.Drawing.Font = e.Font
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e.Graphics.FillRectangle(Brushes.WhiteSmoke, e.Bounds)
End If
Dim oCont As clsContact = DirectCast(sender, System.Windows.Forms.ListBox).Items(e.Index)
If oCont.IsActive Then
textBrush = Brushes.Black
If oCont.IsDirty Then textBrush = Brushes.LightCoral
Else
textBrush = Brushes.LightGray
End If
Dim str = oCont.FullName
e.Graphics.DrawString(str, e.Font, textBrush, e.Bounds, StringFormat.GenericDefault)
e.DrawFocusRectangle()
End Sub
You're using the wrong tools. The List(Of T) doesn't raise any events whatsoever. When bound to a control, the control doesn't know if any items are added/removed/moved. Luckily, the BindingList(Of T) comes to rescue. It will raise a ListChanged event whenever the list is modified. As a bonus, if your class/model implements the INotifyPropertyChanged interface, the control will also reflect property changes.
lbNames.DataSource = New BindingList(Of clsContact)(oCont)
Here's a sample form to show you the basics:
Imports System.ComponentModel
Public Class Form1
Public Sub New()
Me.InitializeComponent()
Me.btnAdd = New Button With {.TabIndex = 0, .Dock = DockStyle.Top, .Height = 30, .Text = "Add new contact"}
Me.btnChange = New Button With {.TabIndex = 1, .Dock = DockStyle.Top, .Height = 30, .Text = "Change random contact name"}
Me.lbContacts = New ListBox With {.TabIndex = 2, .Dock = DockStyle.Fill}
Me.Controls.AddRange({Me.lbContacts, Me.btnChange, Me.btnAdd})
End Sub
Private Sub HandleMeLoad(sender As Object, e As EventArgs) Handles Me.Load
Dim list As New List(Of Contact)
For i As Integer = 1 To 10
list.Add(New Contact With {.Name = String.Format("Contact # {0}", i)})
Next
Me.lbContacts.DataSource = New BindingList(Of Contact)(list)
Me.lbContacts.DisplayMember = "Name"
End Sub
Private Sub HandleButtonAddClick(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim list As BindingList(Of Contact) = DirectCast(Me.lbContacts.DataSource, BindingList(Of Contact))
list.Add(New Contact With {.Name = String.Format("Contact # {0}", (list.Count + 1))})
End Sub
Private Sub HandleButtonChangeClick(sender As Object, e As EventArgs) Handles btnChange.Click
Static rnd As New Random()
Dim list As BindingList(Of Contact) = DirectCast(Me.lbContacts.DataSource, BindingList(Of Contact))
If (list.Count > 0) Then
With list.Item(rnd.Next(0, list.Count))
.Name = Guid.NewGuid().ToString()
End With
End If
End Sub
Public Class Contact
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Public Property Name As String
Get
Return Me.m_name
End Get
Set(value As String)
If (value <> Me.m_name) Then
Me.m_name = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Name"))
End If
End Set
End Property
Private m_name As String
End Class
Private WithEvents btnAdd As Button
Private WithEvents btnChange As Button
Private WithEvents lbContacts As ListBox
End Class
It seems that the solution to fixing this problem is to flip the drawmode back to normal.
adding:
lbNames.DrawMode = DrawMode.Normal
lbNames.DrawMode = DrawMode.OwnerDrawVariable
Fixes the problem.
I guess what is happening is that the drawing handler is becoming disconnected for some reason so the contests of the list are not redrawn when the data source is attached.
My new working code:
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
'lbNames is the listbox carrying all the data
Dim oContacts As List(Of clsContact) = lbNames.DataSource
lbNames.DataSource = Nothing
'Build the new Item, add it to the collection
Dim oNewCont As New clsContact
oNewCont.Editable = True
oNewCont.IsActive = True
oNewCont.Firstname = "Jimmy"
oNewCont.Lastname = "Smith"
oContacts.Add(oNewCont)
' Re-Set up Autocomplete text box
Dim MySource As New AutoCompleteStringCollection()
For Each oc As clsContact In oContacts
MySource.Add(oc.FullName)
Next
txtName.AutoCompleteMode = AutoCompleteMode.Suggest
txtName.AutoCompleteCustomSource = MySource
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource
'Set List Box data back to the collection
lbNames.DataSource = oContacts
lbNames.DisplayMember = "FullName"
lbNames.DrawMode = DrawMode.Normal
lbNames.DrawMode = DrawMode.OwnerDrawVariable
End Sub
The error occurs on the line If wba.Selected = "MARKETING CODE" Then ... Hopefully someone might be able to help. Thanks
'Private Sub NewRecord_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
markCodes = New DataGridViewComboBoxColumn()
markCodes.Name = "Marketing Codes"
Me.DataGridView1.Columns.Add(markCodes)
markCodes.Visible = False
End Sub
'Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)
If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns(wba.Name).Index Then
If TypeOf e.Control Is ComboBox Then
cbxWba = TryCast(e.Control, ComboBox)
RemoveHandler cbxWba.SelectionChangeCommitted, New EventHandler(AddressOf cbxWba_SelectionChangeCommitted)
AddHandler cbxWba.SelectionChangeCommitted, New EventHandler(AddressOf cbxWba_SelectionChangeCommitted) 'this event will fire up if there's a selected item.
End If
End If
End Sub
'Private Sub cbxWba_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs)
loadDescriptions()
End Sub
Private Sub loadDescriptions()
If wba.Selected = "MARKETING CODE" Then
markCodes.Visible = True
Try
Dim con As New SqlConnection
con.ConnectionString = ""
Dim myCommand1 As New SqlClient.SqlCommand
Dim myAdapter1 As New SqlClient.SqlDataAdapter
Dim sql As String = "Select analysis_a + ' - ' + [desc] as Expr1 from marketingCode"
Dim ds As New DataSet
myCommand1.Connection = con
myCommand1.CommandText = sql
myAdapter1.SelectCommand = myCommand1
myCommand1.Connection.Open()
myAdapter1.Fill(ds)
myCommand1.Connection.Close()
Dim dt As New DataTable
dt = ds.Tables(0)
For Each row As DataRow In ds.Tables(0).Rows
DataGridView1.CurrentRow.Cells(description.Name).Value = row("Expr1").ToString()
markCodes.DataSource = dt
markCodes.DisplayMember = "Expr1"
markCodes.ValueMember = "Expr1"
Next
Catch
MsgBox("failed")
End Try
End If
End Sub
What is wba? The .Selected sounds like you are comparing a boolean property to an string.
Looking at your code, I assume that wba is a datagridviewrow, and then you cant use .Selected in that way.
To check for a value of the first selected cell in the datagridview:
With DataGridView1
If .SelectedCells.Count > 0 Then
If .SelectedCells(0).Value = "MARKETING CODE" Then
'we have a hit
End If
End If
End With
or, if you allready know what cell it is and has stored the selected cell in the wba-variable:
If wba.Value = "MARKETING CODE" Then
'we have a hit
End If
If you have selectionmode=fullrowselect and want to check if first cell in the first selected datagridviewrow has a value:
With DataGridView1
If .SelectedRows.Count > 0 Then
If .SelectedRows(0).Cells(0).Value = "MARKETING CODE" Then
'we have a hit
End If
End If
End With
If you have the row stored in the wba, and want to check if the first cell in that row has a specific value then its like this:
If wba.Cells(0).Value = "MARKETING CODE" Then
'we have a hit
End If
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"
dataGridRow.Cells.Add(txt1A)
txt1A.[ReadOnly] = False
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 requestoid() As String
Dim cbo1 As New DataGridViewComboBoxCell()
Dim txt1B As New DataGridViewTextBoxCell()
Dim txt4B As New DataGridViewTextBoxCell()
Dim host As String = txt1B.Value
Dim community As String
community = txt4B.Value
requestoid = New String() {cbo1.Selected}
Dim snmp As New SimpleSnmp
snmp = New SimpleSnmp(host, community)
'abc.Text = txtsnmpaction.SelectedItem
result = snmp.Get(SnmpVersion.Ver1, requestoid)
'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 m getting an error NullReference Exception was handled at this line of code
Dim snmp As New SimpleSnmp
snmp = New SimpleSnmp(host, community)
Kindly help me..!
The nullreference is because the host and community strings are blank. You are setting them to the value of a new instance of a DataGridViewTextBoxCell which is nothing.
Are you trying to set them to a particular row in the datagridview?
If so you should be usging something like:
host = DataGridView1.Rows(1).Cells(1).Value.ToString
community = DataGridView1.Rows(1).Cells(2).Value.ToString