Results 1 to 7 of 7

Thread: multiline textbox to datagridview

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    40

    multiline textbox to datagridview

    Hey there,

    I have a multiline textbox with this appearence:
    Code:
    A#B
    C#
    D#E
    and i want to populate my datagridview to something like this:
    Code:
    A|BC|D|E
    i mean, i want to split when there's a "#" , but i don't want multiline cells in datagridview.

    i tried this code:

    Code:
    Dim sup() As String = TextBox1.Text.Split(vbCr, vbLf, vbTab, " "c, "#")
    DataGridView1.Rows.Add(sup(0), sup(1), sup(2),sup(3)
    but it says it goes outta bounds.. thanks!

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: multiline textbox to datagridview

    Try this.

    VB.NET Code:
    1. Dim sup() As String = TextBox1.Text.Replace(vbCr & vbLf, String.Empty).Split(New String() {"#"}, StringSplitOptions.RemoveEmptyEntries)
    2. DataGridView1.Rows.Add(sup(0), sup(1), sup(2), sup(3))

    - kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: multiline textbox to datagridview

    Quote Originally Posted by KGComputers View Post
    Try this.

    VB.NET Code:
    1. Dim sup() As String = TextBox1.Text.Replace(vbCr & vbLf, String.Empty).Split(New String() {"#"}, StringSplitOptions.RemoveEmptyEntries)
    2. DataGridView1.Rows.Add(sup(0), sup(1), sup(2), sup(3))

    - kgc
    I'd tend to use vbCrLf or, even better, ControlChars.CrLf or, better still, Environment.NewLine rather than 'vbCr & vbLf'. You could also use:
    Code:
    String.Concat(TextBox1.Lines)

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    40

    Re: multiline textbox to datagridview

    "Index out of Range Exception", i get the same error even knowing i have 4 columns.

    http://stackoverflow.com/questions/3...o-datagridview
    Last edited by iDio; Apr 6th, 2016 at 03:36 AM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: multiline textbox to datagridview

    Quote Originally Posted by iDio View Post
    "Index out of Range Exception", i get the same error even knowing i have 4 columns.

    http://stackoverflow.com/questions/3...o-datagridview
    What is the index and what is the range?

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    40

    Re: multiline textbox to datagridview

    i mentioned that in the question.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: multiline textbox to datagridview

    Quote Originally Posted by iDio View Post
    i mentioned that in the question.
    Um, no you didn't. You hadn't seen that exception at that time so how could you have? I'm asking you what the index was that was out of range and what the valid range was when the exception was thrown. It's your code and it's running right in front of you. You can see what those values are when it happens. You can also see where the index came from and therefore determine why it's not within the valid range. This is what debugging is. Debugging is not simply reading your code and hoping to see what's wrong with it. It's watching it in action.

Tags for this Thread

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