Multiple Splitters, Then Adding to Datagridview
Hello,
I am quite stuck. First, I need assitance on creating multiple splitters, then getting that data and adding it into a DataGridView;
Code:
AAL1238:KSEA:KORD:1:1492.9:SEA;
AAL782:KSEA:KORD:1:1492.9;
AAL1848:KSEA:KORD:1:1492.9:;
AAL1360:KSEA:KORD:1:1492.9:;
AAL1804:KSEA:KORD:1:1492.9:;
AAL6796:KSEA:KLAX:13:830.785:;
The First Splitter will be a ':', then the next one will be a ;. Per each line up there, I need to add that content into a row into a datagridview, So if you were to look at the datagridview, It would look like that data above exactly, But separated into the correct columns. Please let me know if you need more detail.
Re: Multiple Splitters, Then Adding to Datagridview
have
vb.net Code:
Private Sub SplitRawData()
'lets as assume you have stored the raw data in to a string array
Dim i, j, k, l As Int16 'used for different loop iterators
Dim RawArray() As String = _
{"AAL1238:KSEA:KORD:1:1492.9:SEA;", "AAL782:KSEA:KORD:1:1492.9;", "AAL1848:KSEA:KORD:1:1492.9:;", _
"AAL1360:KSEA:KORD:1:1492.9:;", "AAL1804:KSEA:KORD:1:1492.9:;", "AAL6796:KSEA:KLAX:13:830.785:;"}
'now : this indicates column seperater
'& ; indicates next row
' & :; indicates convertion to ;
i = RawArray.GetUpperBound(0)
Dim OutPutString() As String
Dim IndexesInOutPutString As Int16
'let first alter :; to ; a place holder 1 row of data
For j = 0 To i - 1
RawArray(j) = RawArray(j).Replace(":;", ";")
Next
'add required number of rows to DGV
Me.DataGridView1.Rows.Add(i + 1)
'now take index by index of the Rawarray and place it in to DGv
'& transpose the values
For j = 0 To i - 1
OutPutString = RawArray(j).Split(":") ' : is the column shift place holder
IndexesInOutPutString = OutPutString.GetUpperBound(0)
With Me.DataGridView1
For l = 0 To IndexesInOutPutString - 1 'row placement
.Rows(k).Cells(l).Value = OutPutString(l).ToString
Next
Array.Clear(OutPutString, 0, j) 'clear the array & reload it
k += 1
End With
Next
End Sub
look at this
Re: Multiple Splitters, Then Adding to Datagridview
Why don't you use the TextFieldParser Class
Simple like this with some changes to handle the different number of elements for each line and the dgv:
vb.net Code:
Dim firstParser As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\file.txt")
firstParser.TextFieldType = FileIO.FieldType.Delimited
firstParser.Delimiters = New String() {":", ";"}
Dim vals as String()
While Not firstParser.EndOfData
vals = firstParser.ReadFields 'Some lines doesn't have the same number of items...
If vals.Length > 6 Then Array.Resize(vals, 6)
DataGridView1.Rows.Add(vals) 'You need to have the columns in the DGV
End While