Results 1 to 3 of 3

Thread: Multiple Splitters, Then Adding to Datagridview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    79

    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.

  2. #2
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Wink Re: Multiple Splitters, Then Adding to Datagridview

    have
    vb.net Code:
    1. Private Sub SplitRawData()
    2.  
    3.         'lets as assume you have stored the raw data in to a string array
    4.         Dim i, j, k, l As Int16 'used for different loop iterators
    5.  
    6.         Dim RawArray() As String = _
    7.         {"AAL1238:KSEA:KORD:1:1492.9:SEA;", "AAL782:KSEA:KORD:1:1492.9;", "AAL1848:KSEA:KORD:1:1492.9:;", _
    8. "AAL1360:KSEA:KORD:1:1492.9:;", "AAL1804:KSEA:KORD:1:1492.9:;", "AAL6796:KSEA:KLAX:13:830.785:;"}
    9.  
    10.         'now : this indicates column seperater
    11.         '& ; indicates next row
    12.         ' & :; indicates convertion to ;
    13.  
    14.         i = RawArray.GetUpperBound(0)
    15.         Dim OutPutString() As String
    16.         Dim IndexesInOutPutString As Int16
    17.  
    18.         'let first alter :; to ; a place holder 1 row of data
    19.         For j = 0 To i - 1
    20.             RawArray(j) = RawArray(j).Replace(":;", ";")
    21.         Next
    22.  
    23.         'add required number of rows to DGV
    24.         Me.DataGridView1.Rows.Add(i + 1)
    25.  
    26.         'now take index by index of the Rawarray and place it in to DGv
    27.         '& transpose the values
    28.  
    29.         For j = 0 To i - 1
    30.             OutPutString = RawArray(j).Split(":") ' : is the column shift place holder
    31.  
    32.             IndexesInOutPutString = OutPutString.GetUpperBound(0)
    33.  
    34.             With Me.DataGridView1
    35.  
    36.  
    37.                 For l = 0 To IndexesInOutPutString - 1  'row placement
    38.                     .Rows(k).Cells(l).Value = OutPutString(l).ToString
    39.                 Next
    40.  
    41.                 Array.Clear(OutPutString, 0, j) 'clear the array & reload it
    42.                 k += 1
    43.             End With
    44.  
    45.         Next
    46.     End Sub
    look at this
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  3. #3
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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:
    1. Dim firstParser As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\file.txt")
    2. firstParser.TextFieldType = FileIO.FieldType.Delimited
    3. firstParser.Delimiters = New String() {":", ";"}
    4. Dim vals as String()
    5.  
    6. While Not firstParser.EndOfData
    7.    vals = firstParser.ReadFields 'Some lines doesn't have the same number of items...
    8.    If vals.Length > 6 Then Array.Resize(vals, 6)
    9.    DataGridView1.Rows.Add(vals) 'You need to have the columns in the DGV
    10. 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
  •  



Click Here to Expand Forum to Full Width