Write program using array instead of data table in vb - vb.net

I have code that displays information about degrees in a datagridview. This program works but I just found out I need to display this information using arrays. Once I figured out how to display it in a data table it was really easy but I need to do it the other way. Some advice please!
Dim array As New DataTable
Dim array2 As New DataTable
Dim array3 As New DataTable
Private Sub btnQuit_Click(sender As System.Object, e As System.EventArgs) Handles btnQuit.Click
Me.Close()
End Sub
Private Sub btnDegrees_Click(sender As System.Object, e As System.EventArgs) Handles btnDegrees.Click
array.Clear()
Dim column1 As DataColumn = New DataColumn("Field of Study")
column1.DataType = System.Type.GetType("System.String")
Dim column2 As DataColumn = New DataColumn("1981")
column2.DataType = System.Type.GetType("System.String")
Dim column3 As DataColumn = New DataColumn("2006")
column3.DataType = System.Type.GetType("System.String")
array.Clear()
array.Columns.Add(column1)
array.Columns.Add(column2)
array.Columns.Add(column3)
array.Rows.Add("Business", "200,521", "318,042")
array.Rows.Add("Computer and info. science", "15,121", "47,480")
array.Rows.Add("Education", "108,074", "107,238")
array.Rows.Add("Engineering", "63,642", "67,045")
array.Rows.Add("Social sciences and history", "100,513", "161,485")
Me.dgvStudies.DataSource = array
End Sub
Private Sub btnChanges_Click(sender As System.Object, e As System.EventArgs) Handles btnChanges.Click
array2.Clear()
Dim column4 As DataColumn = New DataColumn("Field of Study")
column4.DataType = System.Type.GetType("System.String")
Dim column5 As DataColumn = New DataColumn("Change (1981-2006)")
column5.DataType = System.Type.GetType("System.String")
array2.Columns.Add(column4)
array2.Columns.Add(column5)
''Display array two containing percentage changes''
array2.Rows.Add("Computer and info. science", "214.0%")
array2.Rows.Add("Social sciences and history", "60.7%")
array2.Rows.Add("Business", "58.6%")
array2.Rows.Add("Engineering", "5.3%")
array2.Rows.Add("Education", "-0.8%")
dgvStudies.DataSource = array2
End Sub
Private Sub btnHistogram_Click(sender As System.Object, e As System.EventArgs) Handles btnHistogram.Click
array3.Clear()
Dim column6 As DataColumn = New DataColumn("Field of Study")
column6.DataType = System.Type.GetType("System.String")
Dim column7 As DataColumn = New DataColumn("")
column7.DataType = System.Type.GetType("System.String")
Dim column8 As DataColumn = New DataColumn("Degrees in 2006")
column8.DataType = System.Type.GetType("System.String")
array3.Clear()
array3.Columns.Add(column6)
array3.Columns.Add(column7)
array3.Columns.Add(column8)
array3.Rows.Add("Computer and info. science", "*****", "47,480")
array3.Rows.Add("Engineering", "*******", "67,045")
array3.Rows.Add("Education", "***********", "107,238")
array3.Rows.Add("Social sciences and history", "****************", "161,485")
array3.Rows.Add("Business", "********************************", "67,045")
dgvStudies.DataSource = array3
End Sub
End Class

If you want to use an array instead of a DataTable you also need to create an object that corresponds to you DataTable structure. This is an example of your "Degrees implementation" using arrays and a Degrees object.
Sub Main
dim arr = new Degrees() { new Degrees("Business", "200,521", "318,042"), new Degrees("Computer and info. science", "15,121", "47,480")}
Me.dgvStudies.DataSource = arr
End Sub
public class Degrees
public property FieldofStudy As string
public property Year1986 As string
public property Year2006 As string
public sub new(byval fieldofStudy As string, byval year1986 As string, byval year2006 As string)
Me.FieldofStudy = fieldofStudy
Me.Year1986 = year1986
Me.Year2006 = year2006
end sub
end class

Related

Datagridview filter from textbox

i have a vb.net datagridiew and i fill my datagridview add manuel rows
I read too many questions about this questions and answers, but i could't find my answer.
Dim I = .Rows.Add
.Rows(I).Cells(0).Value = "AA"
for example;
COLUMN1 COLUMN2
AA 1
BB 2
so i want filter datagridview from textbox1.text change
What i tried;
TryCast(dgv1.DataSource, DataTable).DefaultView.RowFilter = "COLUMN1 LIKE '%A%'"
Implementation as per comment by #Jimi
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BindGrid()
End Sub
Private Sub BindGrid()
Dim dt As New DataTable
dt.Columns.Add("Column1", GetType(String))
dt.Columns.Add("Column2", GetType(Integer))
DataGridView1.DataSource = dt
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TryCast(DataGridView1.DataSource, DataTable).DefaultView.RowFilter = "COLUMN1 LIKE '%A%'"
End Sub
Create a class which represents a single row.
Public Class MyRow
Public Property Name As String
Public Property Value As Integer
End Class
Set collection of rows to the DataSource fo the DataGridView
Private _rows As List(Of MyRow)
Private Sub Form_Load()
_rows = New List(Of MyRow) From
{
New MyRow With { .Name = "AA", .Value = 1 },
New MyRow With { .Name = "BB", .Value = 2 }
}
myDataGridView.DataSource = _rows
End Sub
Every time value changed in textbox filter collection and set filtered result to the DataSource
Private Sub TextChanged(sender As Object, e As EventArgs) Handles txt.TextChanged
Dim textbox = DirectCast(sender, TextBox)
myDataGridView.DataSource = _rows.
Where(Function(row) row.Name.Contains(textbox.Text)).
ToList()
End Sub
i added second datagridview because in one datagridview always zero record found
Sub DATA_TABLE()
Dim dt As DataTable = New DataTable
Dim dr As DataRow
For COL = 0 To EARSIV_TABLO.ColumnCount - 1
dt.Columns.Add(EARSIV_TABLO.Columns(COL).HeaderText)
Next
For i = 0 To EARSIV_TABLO.RowCount - 1
dr = dt.NewRow()
For COL = 0 To EARSIV_TABLO.ColumnCount - 1
dr(COL) = EARSIV_TABLO.Rows(i).Cells(COL).Value
Next
dt.Rows.Add(dr)
Next
DGV1.DataSource = ""
DGV1.DataSource = dt
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TryCast(DGV1.DataSource, DataTable).DefaultView.RowFilter = "UNVAN LIKE '%" & TextBox1.Text & "%'"
End Sub

Load charts one by one

How can I load Charts one by one. I have made a chart maker function and calling it on page load but I need them to load asynchronously, please help
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
If Not IsPostBack Then
ChartMaker(100288, Chart1, "Series1")
ChartMaker(100289, Chart2, "Series2")
ChartMaker1(100291, Chart3, "Series3")
ChartMaker(100293, Chart4, "series4")
ChartMaker(100295, Chart5, "Series5")
ChartMaker(100278, Chart6, "Series6")
End If
End Sub
Sub ChartMaker(Rnum As Integer, chart As System.Web.UI.DataVisualization.Charting.Chart, Series As String)
Dim Data As String() = Functions.runAnalyticsReportSample(Rnum)
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
chart.Series(Series).XValueMember = "A"
chart.Series(Series).YValueMembers = "B"
chart.Series(Series).IsValueShownAsLabel = True
chart.Series(Series).IsVisibleInLegend = False
chart.DataSource = table
chart.DataBind()
End Sub

Object reference error on chart load (Thread)

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

editing dataTable that has dataRelation using Win Forms

I cannot get around the dataset exception:
"Cannot have a relationship between tables in different DataSets"
form1 creates a dataset, fills 2 dataTables from dataSource, adds them to the dataSet, then creates the dataRelation.
I then set 1 of the dataTables as a property of form2, then showDialog() to open form2. When I try to edit the rows of the dataTable on form2, I get the dataSet exception.
Anyone know why this is happening?
Thanks!
class form1
private dts as object
Public Property currentDts() As object
Get
Return dts
End Get
Set(ByVal value As Object)
dts = value
End Set
End Property
Private Sub form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dim ds as dataset = new dataset()
dim ta as new mainDataSetTableAdapters.myTableAdapter
dim tbl as datatable = ta.getData()
tbl.tablename = "foo"
ds.tables.add(tbl)
dim ta2 as new mainDataSetTableAdapters.myTableAdapter2
dim tbl2 as datatable = ta2.getData()
tbl2.tablename = "bar"
ds.tables.add(tbl2)
ds.relations.add(New dataRelation("bazRelation", _
ds.tables("foo").columns("id"), _
ds.tables("bar").columns("id"),false))
dim dv as dataview = new dataView(ds.tables("bar"))
dv.rowFilter = "Parent(bazRelation).id > 0"
datagridView1.dataSource = dv
me.currentDts = ds
End Sub
Private Sub button1_click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles button1.click
dim ds as dataset = me.currentDts
dim qString as string = "ID = 5 OR ID = 6"
form2.objPassedHere = {ds.tables("bar"), qString}
form2.showDialog()
end sub
end class
class form2
private parentDt as dataTable
private params as object
private bs as bindingSource = new bindingSource()
Public Property objPassedHere() As object
Get
Return params
End Get
Set(ByVal value As Object)
params = value
End Set
End Property
Private Sub form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dim theBarTable as datatable = params(0)
bs.dataSource = theBarTable
bs.filter = params(1)
datagridview1.datasource = bs
end sub
Private Sub textBox1_Leave(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles textBox1.leave
dim rowView as dataRowView = bs.current
dim row as datarow = rowView.row
row.item("personName") = textBox1.text
'ERROR: cannot have a relationship between tables in different datasets!!!!!!
end sub
end class

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