I have lines in a richtextbox with information with separators between them. I want to load it into a datagridview in with columns
ID | Name | Rank | Status
I need to split it by ";".
My code does not use the separator.
Dim Separador As Char = ";"
Dim datagrid As New DataTable
Dim dr As DataRow
'==========================================
datagrid.Columns.Add("ID")
datagrid.Columns.Add("Name")
datagrid.Columns.Add("Rank")
datagrid.Columns.Add("Status")
dr = datagrid.NewRow()
'==========================================
Dim myList1 As New List(Of String)
myList1.AddRange(RichTextBox2.Lines)
DataGridView1.ColumnCount = 1
Dim row As String()
Dim counter As Integer = 0
Dim columnIndex As Integer = 0
'Initialize maximum rows needed
DataGridView1.Rows.Add(1000)
For index = 0 To RichTextBox2.Lines.Count - 1
row = New String() {myList1(index)}
'Fill per row and cell
DataGridView1.Rows(counter).Cells(columnIndex).Value = row(0).ToString()
counter += 1
If counter = 1000 Then
columnIndex += 1
'Add new column
Dim col As New DataGridViewTextBoxColumn
' DataGridView1.Rows.Add(col)
' DataGridView1.Columns.Add(col)
counter = 0
DataGridView1.DataSource = datagrid
End If
Next
You might get more utility by parsing each line of the data into a class; that data can then be stored in a List; and that List can be the DataSource for the DataGridView.
Once you get the hang of it, it's quite easy to manipulate the data with LINQ once it is in a List.
E.g., with RichTextBox1 and DataGridView1 on a form:
Public Class Form1
Public data As New List(Of Datum)
Public Class Datum
Property ID As Integer
Property Name As String
Property Rank As String
Property Status As String
End Class
Sub FillDGV()
DataGridView1.DataSource = data
End Sub
Sub ExtractData()
data.Clear()
For Each line In RichTextBox1.Lines
Dim parts = line.Split(";"c)
If parts.Count = 4 Then
data.Add(New Datum() With {
.ID = CInt(parts(0)),
.Name = parts(1),
.Rank = parts(2),
.Status = parts(3)})
End If
Next
End Sub
Sub FillRTB()
For i = 1 To 10
RichTextBox1.AppendText($"{i};{ChrW(64 + i)};{10 - i};✓" & vbLf)
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillRTB()
ExtractData()
FillDGV()
End Sub
End Class
Related
I'm trying to retrieve data from the web to desktop using vbnet, but whenever i tried to show it on datagridview it show this error.
It only succes when i only show the first column.
But when it comes to add more column it will show at first but have error like this.
And when i tried to run it again it have error like this.
The code i use :
Imports System.Net
Imports System.IO
Public Class Form1
Dim strArr() As String
Dim strArr1() As String
Dim count, c1 As Integer
Dim str, str2 As String
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim k As Integer = 0
Dim uri As New Uri("http://localhost/tampil.php")
If (uri.Scheme = Uri.UriSchemeHttp) Then
Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
request.Method = WebRequestMethods.Http.Get
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(response.GetResponseStream())
Dim pagehtml As String = reader.ReadToEnd()
response.Close()
str = pagehtml
strArr = str.Split(";")
For count = 0 To strArr.GetUpperBound(0)
ReDim Preserve strArr1(k)
strArr1(k) = strArr(count)
str2 = strArr1(k)
Dim words As String() = strArr1(k).Split(New Char() {"-"c})
If str2 = "" Then
Exit For
End If
DataGridView1.Rows.Add("")
DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(0).Value = words(0)
DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(1).Value = words(1)
DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(2).Value = words(2)
DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(3).Value = words(3)
DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(4).Value = words(4)
k += 1
Next
End If
End Sub
End Class
Data from web
Can anyone help ?
Adding a row will return the index of the new row.
You can use that when updating the cell values.
This method will add the rows as you'd expect.
If there is any chance of values being returned from the html query that have a different number of elements, you should check that.
Private Sub LoadDataGridViewFromHTML(htmlValue As String)
Dim entities() As String = htmlValue.Split(Convert.ToChar(";"))
For Each entityItem As String In entities
If Not String.IsNullOrEmpty(entityItem) Then 'check you're not dealing with an empty string
Dim entityValues() As String = entityItem.Split(Convert.ToChar("-"))
Dim newRowId As Integer = DataGridView1.Rows.Add("")
DataGridView1.Rows(newRowId).Cells(0).Value = entityValues(0)
DataGridView1.Rows(newRowId).Cells(1).Value = entityValues(1)
DataGridView1.Rows(newRowId).Cells(2).Value = entityValues(2)
DataGridView1.Rows(newRowId).Cells(3).Value = entityValues(3)
DataGridView1.Rows(newRowId).Cells(4).Value = entityValues(4)
End If
Next
End Sub
I am kinda new at coding.
I am reading some numbers from a txt files and i have them into a array,i managed to make them descending,now i want to make a sum with this numbers,for example:
I will have an input number 1000,on the list i will have numbers from 100 to 1000 and i want to make a sum with them to reach 1000 with minimum rest.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fStream As New System.IO.FileStream("C:\Users\Alex\Desktop\test.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
Dim List As New List(Of Double)
Do While sReader.Peek >= 0
List.Add(sReader.ReadLine)
Loop
Dim i As Long
Dim txt As String
Dim thisArray As Double() = List.ToArray
For i = LBound(thisArray) To UBound(thisArray)
txt = thisArray(i) & vbCrLf
Next i
Array.Sort(thisArray)
For j As Integer = 0 To thisArray.Count - 1
Dim FILE_NAME As String = "C:\Users\Alex\desktop\test2.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, IO.FileMode.Append)
objWriter.WriteLine(thisArray(j))
objWriter.Close()
Else
MessageBox.Show("File Does Not Exist")
End If
Next
fStream.Close()
sReader.Close()
End Sub
I dont know where to start to do this..input number will be from a textbox.
This is how my datagridview looks like:
ID | Name | Course|
1 | AB | IT
2 | CD | IT
3 | EF | CS
4 | GH | BA
5 | IJ | IT
It suppose to get the values of the duplicated strings of the column "course". So, the result suppose to look like this:
(label) IT = 3(label)
(label) CS = 1(label)
(label) BA = 1(label)
(label) IST = 0(label)
Here's my code.
Private Sub CounterButton_Click(sender As System.Object, e As System.EventArgs) Handles CounterButton.Click
Dim countertry As String = 0
Dim result As String = 0
Dim IT As Integer = 1
Dim IST As String = 1
Dim CS As String = 1
Dim BA As String = 1
For c As Integer = 0 To StudentInfoTableDataGridView.RowCount - 1
countertry += StudentInfoTableDataGridView.Rows(c).Cells(3).Value
' Im so lost here
' If IT it will show IT = +1
' If IS it will show IST = +1
' If CS it will show CS = +1
' If BA it will show BA = +1
Next
CountText1.Text = result '(for IT)
CountText2.Text = result '(for IST)
CountText3.Text = result '(for CS)
CountText4.Text = result '(for BA)
End Sub
I think you could just use Dictionary and add the item value if the current string is the same as the key.
Something like this
Private Sub CounterButton_Click(sender As System.Object, e As System.EventArgs) Handles CounterButton.Click
Dim result As String = 0
Dim courseDict As new Dictionary<string, integer>
Dim currString As String
For c As Integer = 0 To StudentInfoTableDataGridView.RowCount - 1
currString = StudentInfoTableDataGridView.Rows(c).Cells(3).Value
If courseDict.ContainsKey(currString) Then
courseDict(currString) += 1
Else
courseDict.Add(currString,0)
End If
Next
CountText1.Text = currString("IT")
CountText2.Text = currString("IST")'(for IST)
CountText3.Text = currString("CS")'(for CS)
CountText4.Text = currString("BA")'(for BA)
End Sub
You could try this approach. It depends on how you are populating the datatable. But you could try and create a dictionary then compare the datagrid to the dictionary
Find duplicates in a Datagridview
The following groups rows by courses and places results into labels. I have commented the code in regards to label names for this to work. Also note I created columns via the DataGridView's designer.
''' <summary>
''' Columns for DataGridView are defined in the IDE
''' There is one label per count where labels are named
''' Label'ss' e.g. LabelIT, LabelCS etc
''' </summary>
''' <remarks></remarks>
Public Class Form1
Private labelList As New List(Of Label)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add(New Object() {1, "AB", "IT"})
DataGridView1.Rows.Add(New Object() {2, "CD", "IT"})
DataGridView1.Rows.Add(New Object() {3, "EF", "CS"})
DataGridView1.Rows.Add(New Object() {4, "GH", "BA"})
DataGridView1.Rows.Add(New Object() {5, "IJ", "IT"})
labelList = New List(Of Label) _
From {LabelIT, LabelCS, LabelBA, LabelIST}
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim labelItem As Label = Nothing
' set all label text to zero in the event
' one or more labels are not in a grouping
For Each lb In labelList
lb.Text = "0"
Next
' one liner to group by last column and provide count
Dim results =
(
From row In DataGridView1.Rows.Cast(Of DataGridViewRow)()
Where Not row.IsNewRow) _
.GroupBy(Function(row) row.Cells("CourseColumn").Value) _
.Select(
Function(iGroupRow)
Return New With
{
Key .Name = iGroupRow.Key,
.Count = iGroupRow.Count
}
End Function)
For Each item In results
labelItem = labelList.Where(
Function(lb) lb.Name.EndsWith(item.Name.ToString)
).FirstOrDefault
If labelItem IsNot Nothing Then
labelItem.Text = item.Count.ToString
End If
Next
End Sub
End Class
How would one select item in listview (first column) that is most similar to string value from e.g. label or textbox.
Listview is populated with this code :
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListView1.Items.Clear()
ListView1.View = System.Windows.Forms.View.Details
ListView1.Columns.Add("COL1", 100, HorizontalAlignment.Left) 'KONTO
ListView1.Columns.Add("COL2", 140, HorizontalAlignment.Left) 'NAZIV
Dim FilePath As String = "W:\GLAVNI\KOR14\"
Dim DBF_File As String = "MATIKGL"
Dim ColName As String = "KONTO"
'Dim naz As String
Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _
" ;Extended Properties=dBASE IV")
con.Open()
Using cmd As New OleDbCommand("SELECT * FROM MATIKGL ORDER BY KONTO, NAZIV", con)
Using reader As OleDbDataReader = cmd.ExecuteReader()
If reader.HasRows Then
While (reader.Read())
Me.ListView1.Items.Add(reader("KONTO"))
'ListView1.Items(i).SubItems.Add(rdr.Item("YourColumnName").ToString)
'BELOW SELECTS ALL ITEMS THAT STARTS WITH 2020-
For i = 0 To ListView1.Items.Count - 1
If ListView1.Items(i).ToString.Contains("2020-") Then
Else
ListView1.Items.Remove(ListView1.Items(i))
End If
Next
End While
Else
End If
End Using
End Using
con.Close()
End Using
End Sub
I have one textbox and a button.
Textual input from textbox should be compared with all items in listview and closest should be selected. One more thing : All items are sorted alphabetically
Button code is :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListView1.MultiSelect = False
ListView1.FullRowSelect = True
Dim checkInt As Integer = FindItem(ListView1, "2020-" & TextBox1.Text)'this one is changed since all items starts with "2020-"& UCASE TEXT
If checkInt <> -1 Then
ListView1.Items(checkInt).Selected = True
ListView1.Focus()
Else
Label1.Text = "Search string not found"
End If
End Sub
UPDATED CODE
Dim checkInt As Integer = FindItem(ListView1, "2020-" & TextBox3.Text)
If checkInt <> -1 Then
TextBox4.Focus()
Else
Label14.Text = "NEMA"
On Error GoTo ext
Dim li As ListViewItem
ListView1.SelectedItems.Clear()
ListView1.HideSelection = False
li = ListView1.FindItemWithText("2020-" & UCase(TextBox3.Text))
If Not (li Is Nothing) Then
Me.ListView1.Focus()
li.Selected = True
li.EnsureVisible()
ElseIf li Is Nothing Then
li = ListView1.FindItemWithText("2020-" & Strings.Left(TextBox3.Text, 1))
Me.ListView1.Focus()
li.Selected = True
li.EnsureVisible()
Else
End If
Exit Sub
ext:
TextBox3.Text = ""
TextBox3.Focus()
Label14.Text = "String not found"
End If
This one works.
I know it's not the best solution but it's working.
Could fixed this without your help, thank you Phillip Trelford
Define a function to score two strings for closeness then use LINQ to find the lowest score, i.,e.
' Example score function
Function Score(a As String, b As String) As Integer
Dim index = 0
While index < a.Length And index < b.Length
Dim diff = Math.Abs(AscW(a(index)) - AscW(b(index)))
If diff <> 0 Then Return diff
index += 1
End While
Return Math.Abs(a.Length - b.Length)
End Function
Function Closest(searchWord As String, words As String()) As String
Dim ordered =
From w In words
Select Word = w, Score = Score(w, searchWord)
Order By Score
Return ordered.First().Word
End Function
Sub Main()
Dim words = {"Alpha", "Apple", "Ask"}
Dim searchWord = "Ann"
Dim word = Closest(searchWord, words)
Console.WriteLine(word)
End Sub
Update
To select the value in a WinForms ListView, you need to do roughly this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListView1.MultiSelect = False
ListView1.FullRowSelect = True
Dim prefix = "2020-"
' Extract items from Listview
Dim items = New List(Of String)()
For Each item In ListView1.Items
items.Add(item)
Next
Dim words = items.ToArray()
Dim searchWord = TextBox1.Text
Dim resultWord = Closest(searchWord, words)
'this one is changed since all items starts with "2020-"& UCASE TEXT
Dim checkInt As Integer = FindItem(ListView1, prefix & resultWord)
If checkInt <> -1 Then
ListView1.Items(checkInt).Selected = True
ListView1.Focus()
Else
Label1.Text = "Search string not found"
End If
End Sub
I have a single Row in DataGridView which shows different results for the selected Oids..The problem is that the next result replaces the first one in the same row..Is there any way to get next result in next rows so that DataGridview can show previous results also..my datagrid is not bound to any data source.
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
Replace...
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()
With something like this...
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)
This will create a new row with those values instead of overwriting the existing values.