I need help to send a RichTextBox to a datagridview

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1033 Views - Last Post: 26 December 2018 - 07:50 AM Rate Topic: -----

#1 fulltower   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-December 18

I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 06:57 AM

I need help, to send a RichTextBox to a datagridview en vb.net


at RichTextBox = txtresult:

Maria|perez|35 years|married
jose|lopes|40years|divorced
vanessa|fernandez|20 years|single
luis|gomez|21 years|single

https://prnt.sc/lxh36s


to a:

at datagridview:
datagrid.Columns.Add ("Name")
datagrid.Columns.Add ("Last name")
datagrid.Columns.Add ("Age")
datagrid.Columns.Add ("Status")

https://prnt.sc/lxgx2i

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: I need help to send a RichTextBox to a datagridview

#2 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 312
  • View blog
  • Posts: 1,181
  • Joined: 02-November 11

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 07:16 AM

Start by seperating the data rows, something like:
String[] dataRows = myString.Split(new String() {Environment.NewLine}, StringSplitOptions.None)


Then you need the column data, so something like:
For Each item as String in dataRows
    String[] columnData = dataRow.Split('|')
    Me.DataGridView1.Rows.Add(columnData)
Next


That may not be perfect but it'll get you on track. I've not written in VB in a long time now and that was done from memory.
Was This Post Helpful? 0
  • +
  • -

#3 fulltower   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-December 18

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 02:05 PM

View PostTechnoBear, on 20 December 2018 - 07:16 AM, said:

Start by seperating the data rows, something like:
String[] dataRows = myString.Split(new String() {Environment.NewLine}, StringSplitOptions.None)


Then you need the column data, so something like:
For Each item as String in dataRows
    String[] columnData = dataRow.Split('|')
    Me.DataGridView1.Rows.Add(columnData)
Next


That may not be perfect but it'll get you on track. I've not written in VB in a long time now and that was done from memory.


What am I doing wrong? it does not work

Dim Separador As Char
                Dim datagrid As New DataTable
                Dim dr As DataRow
                '==========================================

                datagrid.Columns.Add("name")
                datagrid.Columns.Add("las name ")
                datagrid.Columns.Add("age")
                datagrid.Columns.Add("status")
                dr = datagrid.NewRow()
                '========================================== 

                Separador = "|"
                datagrid.Rows.Add(txtResult.Text.Split(Separador))

                DataGridView1.DataSource = datagrid

                DataGridView1.CurrentRow.Selected = True 

 

Was This Post Helpful? 0
  • +
  • -

#4 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 312
  • View blog
  • Posts: 1,181
  • Joined: 02-November 11

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 03:14 PM

What doesn't work? What is it doing that it's not supposed to? What is it not doing that it's supposed to? What have you looked at so far? Have you tried debugging to see where the issue may be?
Was This Post Helpful? 0
  • +
  • -

#5 fulltower   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-December 18

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 03:55 PM

View PostTechnoBear, on 20 December 2018 - 03:14 PM, said:

What doesn't work? What is it doing that it's not supposed to? What is it not doing that it's supposed to? What have you looked at so far? Have you tried debugging to see where the issue may be?



this is the code:

Private Sub PsButton7_Click(sender As Object, e As EventArgs) Handles PsButton7.Click



        Select Case ComboBox1.SelectedIndex
            Case 0

                Dim Separador As Char
                Dim datagrid As New DataTable
                Dim dr As DataRow
                '==========================================

                datagrid.Columns.Add("name")
                datagrid.Columns.Add("las name ")
                datagrid.Columns.Add("age")
                datagrid.Columns.Add("status")
                dr = datagrid.NewRow()
                '========================================== 

                Separador = "|"
                datagrid.Rows.Add(txtResult.Text.Split(Separador))

                DataGridView1.DataSource = datagrid

                DataGridView1.CurrentRow.Selected = True



it does not give me line of error, when I press the send button, it does not send the text to the datagrid and it closes the program
Was This Post Helpful? 0
  • +
  • -

#6 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 312
  • View blog
  • Posts: 1,181
  • Joined: 02-November 11

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 05:09 PM

Get rid of the datarow and datagrod objects you created, they aren't needed. Just for a start. Just add the rows directly to the datagrod, it'll save steps and reduce memory load.

Take another look at the code examples I gave above and see how I implemented the same idea.
Was This Post Helpful? 0
  • +
  • -

#7 fulltower   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-December 18

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 05:29 PM

View PostTechnoBear, on 20 December 2018 - 05:09 PM, said:

Get rid of the datarow and datagrod objects you created, they aren't needed. Just for a start. Just add the rows directly to the datagrod, it'll save steps and reduce memory load.

Take another look at the code examples I gave above and see how I implemented the same idea.



I did this:

 Private Sub PsButton7_Click(sender As Object, e As EventArgs) Handles PsButton7.Click



        Select Case ComboBox1.SelectedIndex
            Case 0


                'Dim Separador As Char
                Dim datagrid As New DataTable
                Dim dr As DataRow
                '==========================================

                datagrid.Columns.Add("name")
                datagrid.Columns.Add("las name ")
                datagrid.Columns.Add("age")
                datagrid.Columns.Add("status")
                dr = datagrid.NewRow()



                Dim Cadena As String = txtResult.Text
                Dim ArrCadena As String() = Cadena.Split("|")

                datagrid.Rows.Add(ArrCadena)

                DataGridView1.DataSource = datagrid



when executing the program and pressing the send button, this message appears:

System.ArgumentException: 'The input matrix is ​​longer than the number of columns in this table.'


any solution? help me please ://>
Was This Post Helpful? 0
  • +
  • -

#8 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 312
  • View blog
  • Posts: 1,181
  • Joined: 02-November 11

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 06:00 PM

Stop posting the same code. Look at what I have already given you and look at the differences.

I have given you everything already, try applying it first and then we can move forwards.
Was This Post Helpful? 0
  • +
  • -

#9 fulltower   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-December 18

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 06:20 PM

View PostTechnoBear, on 20 December 2018 - 06:00 PM, said:

Stop posting the same code. Look at what I have already given you and look at the differences.

I have given you everything already, try applying it first and then we can move forwards.


Ok, it's fine... sorry

Private Sub PsButton7_Click(sender As Object, e As EventArgs) Handles PsButton7.Click



        Select Case ComboBox1.SelectedIndex
            Case 0

                'Start by seperating the data rows, something Like
                String[] dataRows = myString.Split(New String() {Environment.NewLine}, StringSplitOptions.None)


                'Then you need the column data, so something Like 

                For Each item As String In dataRows

                    String[] columnData = dataRow.Split("|")

                    Me.DataGridView1.Rows.Add(columnData)

                Next


        End Select
    End Sub



It gives me this lines of errors:

Error BC30203 An identifier was expected.
Error BC30203 An identifier was expected.
Error BC30109 'String' is a type of class and can not be used as an expression.
Error BC30109 'String' is a type of class and can not be used as an expression.
Error BC30451 'dataRows' is not declared. It may be inaccessible due to its level of protection.
Error BC30451 'columnData' is not declared. It may be inaccessible due to its level of protection.
Was This Post Helpful? 0
  • +
  • -

#10 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 312
  • View blog
  • Posts: 1,181
  • Joined: 02-November 11

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 06:27 PM

What do you think those errors mean? How do you think they would be fixed?
Was This Post Helpful? 0
  • +
  • -

#11 fulltower   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-December 18

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 06:40 PM

View PostTechnoBear, on 20 December 2018 - 06:27 PM, said:

What do you think those errors mean? How do you think they would be fixed?


making the declaration of those variables, but I have no idea, I am very noob :cry2: :cry2: :cry2: :cry2:
Was This Post Helpful? 0
  • +
  • -

#12 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 312
  • View blog
  • Posts: 1,181
  • Joined: 02-November 11

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 07:03 PM

[VariableName] As [VariableType]
Was This Post Helpful? 1
  • +
  • -

#13 fulltower   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-December 18

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 07:48 PM

View PostTechnoBear, on 20 December 2018 - 07:03 PM, said:

[VariableName] As [VariableType]



Is it like that?
 Dim dataRows As String = txtResult.Text
 Dim columnData As String() = dataRows.Split("|")


now only this line of errors come out:
Error BC30203 An identifier was expected.
Error BC30203 An identifier was expected.
Error BC30109 'String' is a type of class and can not be used as an expression.
Error BC30109 'String' is a type of class and can not be used as an expression.

String[]

Was This Post Helpful? 0
  • +
  • -

#14 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 195
  • View blog
  • Posts: 1,089
  • Joined: 05-December 13

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 08:29 PM

Firstly, turn on Option Strict and Option Explicit
To do that, put these two lines at the top of your class files.
Option Strict On
Option Explicit On


Second, you need to properly convert your data types. That includes symbols used in strings.

Dim columnData As String() = dataRows.Split(CType("|", Char()))

And this, looks like it came from C# so ditch it.
String[] dataRows = myString.Split(New String() {Environment.NewLine}, StringSplitOptions.None)

Rather than taking code from people online, would you not be best grabbing a book to study the basics. So much so; that you know what language people are giving you code snippets in...

In C# we declare arrays as string[] arr = ...etc but this is not C#. This is VB.Net

Also, do you know why you have wrote it like this :: Dim columnData As String()?
Was This Post Helpful? 0
  • +
  • -

#15 fulltower   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-December 18

Re: I need help to send a RichTextBox to a datagridview

Posted 20 December 2018 - 09:11 PM

View PostSheepings, on 20 December 2018 - 08:29 PM, said:

Firstly, turn on Option Strict and Option Explicit
To do that, put these two lines at the top of your class files.
Option Strict On
Option Explicit On


Second, you need to properly convert your data types. That includes symbols used in strings.

Dim columnData As String() = dataRows.Split(CType("|", Char()))

And this, looks like it came from C# so ditch it.
String[] dataRows = myString.Split(New String() {Environment.NewLine}, StringSplitOptions.None)

Rather than taking code from people online, would you not be best grabbing a book to study the basics. So much so; that you know what language people are giving you code snippets in...

In C# we declare arrays as string[] arr = ...etc but this is not C#. This is VB.Net

Also, do you know why you have wrote it like this :: Dim columnData As String()?

if it contains ("|")??
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2