|
-
Feb 27th, 2011, 06:22 PM
#1
Thread Starter
Lively Member
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.
-
Feb 28th, 2011, 12:42 AM
#2
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
-
Feb 28th, 2011, 05:14 AM
#3
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
Last edited by mickey_pt; Feb 28th, 2011 at 05:45 AM.
Reason: Forgot var... :)
Rate People That Helped You
Mark Thread Resolved When Resolved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|