I need help to send a RichTextBox to a datagridview

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 - 11:08 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()?



I have a problem..

When I put a line in the txtResult and press the send button if it reaches the datagrid, but when I place in the textResult 4 lines and press the send button I get this message:

System.ArgumentException: 'La matriz de entrada es más larga que el número de columnas en esta tabla.'

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

        Select Case ComboBox1.SelectedIndex
            Case 0


                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 dataRows As String = txtResult.Text

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

                datagrid.Rows.Add(columnData)

                DataGridView1.DataSource = datagrid

                DataGridView1.CurrentRow.Selected = True

        End Select
    End Sub


you can help?
Was This Post Helpful? 0
  • +
  • -

#17 andrewsw   User is offline

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,308
  • Joined: 12-December 12

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

Posted 21 December 2018 - 04:43 AM

For those that don't, like myself, read Spanish, Google translate says:

"
The input matrix is ​​longer than the number of columns in this table."

What do you think this is saying? I think it is saying that the number of items in the (split) array is more than the number of columns you are trying to feed into.

Note that you do not need to quote a previous post every time, there is a Reply button further down the page.
Was This Post Helpful? 0
  • +
  • -

#18 TechnoBear   User is online

  • Lady A
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,149
  • Joined: 02-November 11

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

Posted 21 December 2018 - 06:49 AM

Sheepings, I keep trying to write my examples in VB but it's been so long since I used VB and my current project is C# so I keep mixing them up.

Also, I forget that new people can't always do a straight translate between the two.
Was This Post Helpful? 0
  • +
  • -

#19 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,025
  • Joined: 05-December 13

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

Posted 21 December 2018 - 07:05 AM

haha I know @TechnoBear, don't worry about it. That happens to some people, even the best of us. I mostly write in C# myself, but for some reason I am not one of those people who confuse the two.

@OP Sorry, I don't provide support on 1990's controls such as what you're using, but maybe someone else will help you further or perhaps you might find the two answers here useful. https://social.msdn....m=csharpgeneral
Was This Post Helpful? 0
  • +
  • -

#20 TechnoBear   User is online

  • Lady A
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,149
  • Joined: 02-November 11

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

Posted 21 December 2018 - 07:19 AM

He's not splitting on the new line so instead of:

Title, Title, Title, Title
  X      X      X      X
  X      X      X      X
  X      X      X      X


He is getting:

Title, Title, Title, Title
  X      X      X      X      X      X      X      X      X      X      X      X


Hence the error.

The reason I kept pointing him back to my original answer is because it split by new line first and then by the separator.

This post has been edited by TechnoBear: 21 December 2018 - 07:19 AM

Was This Post Helpful? 0
  • +
  • -

#21 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 21 December 2018 - 07:48 AM

@TechnoBear How can I make it to be the first image you put on?
Was This Post Helpful? 0
  • +
  • -

#22 TechnoBear   User is online

  • Lady A
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,149
  • Joined: 02-November 11

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

Posted 21 December 2018 - 08:13 AM

this is where my terribly formatted hybrid mess of a code example come in.

What you need to do is the following (to save creating another unhelpful code example I shall present the flow in pseudo):

Get the string from the text box. As presented this would be [Data separated by '|'][New Line][Data separated by '|']...
Split the string by NewLine to get a String() array of [Data separated by '|']
loop through the array. A for loop would be good for this.
Within the loop:
Split each [Data separated by '|'] element by the seperator.
Add the resulting String() array to the datagrid via Datagrid.Rows.Add(Data)
End Loop

Voila! Should be as simple as that.

What you are missing currently is the split by new line.

This post has been edited by TechnoBear: 21 December 2018 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

#23 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 22 December 2018 - 10:27 PM

someone who can help me
Was This Post Helpful? 0
  • +
  • -

#24 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,025
  • Joined: 05-December 13

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

Posted 23 December 2018 - 03:05 PM

I know this works, but because I have a sincere passionate hate for DGV and everything DGV related, I've left out binding sources, and done it my own way. Other will tell you that you are better to bind your data when working with DGV...

Anyway, I need to point out that you have an error in how you receive your data into a richtextbox. Your RTB has 4 sections each separated by a "|", but on the end of each relationship section of the string, there is no trailing "|" for each row you want to insert into your DGV. So as a result of this, your data will insert wrongly to the wrong fields of your DGV rows because of where you are splitting into an array of these strings.
Dim lines As String() = rtb.Text.Split({"|"}, StringSplitOptions.None)
And the problem here is after each relationship status, there is no trailing "|" ::
Maria|perez|35 years|married
jose|lopes|40years|divorced
vanessa|fernandez|20 years|single
luis|gomez|21 years|single

And as a result you end up with this ::

Attached Image

As you can see, the structure gets all messed up, but the fix to that is to ensure that after each of the relationships status's have the trailing "|". Also notice, if you use a trailing "|" on the very end of the string, you will end up with an Empty string, this is also something you don't want, so don't add one of these "|" to the end of the string. As you can see this while debugging your code ::

Attached Image

Also note that you have some vbCrLf from line breaking your string, and you can avoid that by putting your string on one line like this ::
        rtb.Text = "Maria|perez|35 years|married|jose|lopes|40 years|divorced|vanessa|fernandez|20 years|single|luis|gomez|21 years|single"

As opposed to the way you posted it ::
"Maria|perez|35 years|married|
jose|lopes|40 years|divorced|
vanessa|fernandez|20 years|single|
luis|gomez|21 years|single"
Attached Image

For cleaner code, formatting is important not to line break by pressing shift enter etc.

So lets step though the code...
    Public iCell As Integer = -1
iCell has an offset of -1 because we want to be able to index from 0 and not 1. So when we get into our loop, we will increase this value for each increment of each section we made a split in the array
Dim lines As String()
We then have iRow which we will use to track what row we are working with inside the DGV ::
dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
and so we declared it as ::
    Public iRow As Integer = 0
We then add our button which will do all the work for us, but you can move this code to where ever you find fitting in your application ::
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim rtb As New RichTextBox
        rtb.Text = "Maria|perez|35 years|married|jose|lopes|40 years|divorced|vanessa|fernandez|20 years|single|luis|gomez|21 years|single"
        Dim lines As String() = rtb.Text.Split({"|"}, StringSplitOptions.None)
        For Each line As String In lines
            iCell = iCell + 1
            Console.WriteLine(line)
            Select Case GetSetDG.cellGetSetter
                Case 0
                    dgv.Rows.Add()
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                Case 1
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                Case 2
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                Case 3
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                    If GetSetDG.cellGetSetter = 3 Then
                        GetSetDG.rowGetSetter = GetSetDG.rowGetSetter + 1
                        GetSetDG.cellGetSetter = -1
                    End If
            End Select
        Next
    End Sub
And we will explain what the button is doing... First I've declared a RichTextBox called rtb, but you can remove this, and be sure to name any reference in the source code of rtb to the actual name of your rtb control. I then set the text property of the rtb control ::
        rtb.Text = "Maria|perez|35 years|married|jose|lopes|40 years|divorced|vanessa|fernandez|20 years|single|luis|gomez|21 years|single"

And then we do our splitting at the "|" character, and add it to our list.
        Dim lines As String() = rtb.Text.Split({"|"}, StringSplitOptions.None)

Then we loop through each of the lines as follows ::
For Each line As String In lines
At each increment, we increase the cell by 1 like this (We will get to GetSetDG further down, follow along.) ::
GetSetDG.cellGetSetter = GetSetDG.cellGetSetter + 1
and this allows us to move to the next cell on that row we are setting text on. We will get to that in a moment...

I've used a select statement as its more convenient in this set of circumstances ::
Select Case GetSetDG.cellGetSetter
                Case 0
For each case to be accessible, will require this integer to be equal to the number for that case ::
GetSetDG.cellGetSetter
In order to set one of the cells, we need a row to work with, so lets add one ::
dgv.Rows.Add()
Next, we want to update the cell where the row we are on is the integer of
GetSetDG.rowGetSetter
and the cell is the integer of
GetSetDG.cellGetSetter
and by using
[code].Value = line
we are able to set the value of that cell to the value of the line string we are iterating in our for each loop.
For Each line As String In lines
And we do this for the remainder of the Select Statement ::
Select Case GetSetDG.cellGetSetter
                Case 0
                    dgv.Rows.Add()
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                Case 1
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                Case 2
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                Case 3
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                    If GetSetDG.cellGetSetter = 3 Then
                        GetSetDG.rowGetSetter = GetSetDG.rowGetSetter + 1
                        GetSetDG.cellGetSetter = -1
                    End If
            End Select
Except you will notice we set some integer values in case 3. We increase
GetSetDG.rowGetSetter = GetSetDG.rowGetSetter + 1
for the sole reason that we may want to set more cells but on the next row. So this increments the row, but then we also want to ensure we reset the cells integer value back to -1 so that when this method next runs, we will be indexing those cells from 0 again and thus allowing our select statements cases to be met and set appropriately for the next row in your DGV. ::
GetSetDG.cellGetSetter = -1
For good practices, I always work with classes and allow by interfaces or GUI to always be updated by my classes.
Public Class GetSetDG
    Public Shared Property cellGetSetter() As Integer
        Get
            Return Form1.iCell
        End Get
        Set(ByVal v As Integer)
            Form1.iCell = v
        End Set
    End Property
    Public Shared Property rowGetSetter() As Integer
        Get
            Return Form1.iRow
        End Get
        Set(ByVal v As Integer)
            Form1.iRow = v
        End Set
    End Property
End Class
It is pretty self explanatory, and all we are doing is either getting or setting the variables of Form1.iRow and Form1.iCell, from within this class. For one example; if we do this :: GetSetDG - we are calling the class, and so when we do this :: GetSetDG.rowGetSetter - we are calling the .rowGetSetter property on that class. If we equal a value to this property, we setting the value, but if we equal a variable or controls text property to this property, we are getting the value and applying it to the object (whatever that control/object may be.)

Now, if you followed the advice step by step, you should have something that looks like this ::

Attached Image

And the completed source; note, I have option strict and explicit enabled as I advised previous in this post, (I believe). Tested and working, looks like this ::
Option Strict On
Option Explicit On

Public Class Form1
    Public iCell As Integer = -1
    Public iRow As Integer = 0
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim rtb As New RichTextBox
        rtb.Text = "Maria|perez|35 years|married|jose|lopes|40 years|divorced|vanessa|fernandez|20 years|single|luis|gomez|21 years|single"
        Dim lines As String() = rtb.Text.Split({"|"}, StringSplitOptions.None)
        For Each line As String In lines
            GetSetDG.cellGetSetter = GetSetDG.cellGetSetter + 1
            Select Case GetSetDG.cellGetSetter
                Case 0
                    dgv.Rows.Add()
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                Case 1
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                Case 2
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                Case 3
                    dgv.Rows(GetSetDG.rowGetSetter).Cells(GetSetDG.cellGetSetter).Value = line
                    If GetSetDG.cellGetSetter = 3 Then
                        GetSetDG.rowGetSetter = GetSetDG.rowGetSetter + 1
                        GetSetDG.cellGetSetter = -1
                    End If
            End Select
        Next
    End Sub
End Class
Public Class GetSetDG
    Public Shared Property cellGetSetter() As Integer
        Get
            Return Form1.iCell
        End Get
        Set(ByVal v As Integer)
            Form1.iCell = v
        End Set
    End Property
    Public Shared Property rowGetSetter() As Integer
        Get
            Return Form1.iRow
        End Get
        Set(ByVal v As Integer)
            Form1.iRow = v
        End Set
    End Property
End Class


Happy Christmas by the way. ;)

Edited, and cleaned up. - One last thing, case 3 won't execute unless its 3, and so, its a little redundant having the if statement there, and so you can simply remove it but don't remove the variables being set.

This post has been edited by Sheepings: 23 December 2018 - 03:35 PM

Was This Post Helpful? 2
  • +
  • -

#25 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 23 December 2018 - 04:22 PM

rtb.Text = "Maria|perez|35 years|married|jose|lopes|40 years|divorced|vanessa|fernandez|20 years|single|luis|gomez|21 years|single"
 


I can use this? why do I need to show the result of txt

rtb.Text = txtResult.text

Was This Post Helpful? 0
  • +
  • -

#26 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,025
  • Joined: 05-December 13

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

Posted 23 December 2018 - 04:37 PM

I can't answer that. Since I don't know what you are doing with txtResult.Text. But I will assume you are probably querying something that gives you the result in txtResult.Text?

I suggest executing the source I provided, study it, and decide how you are best to utilize the functionality you were trying to achieve. I only wrote it out as you described it as what you wanted to do on your opening post. Also, be sure to mark any posts you find helpful. You'll find my last post, has everything you need to achieve what you wanted.

Good luck
Was This Post Helpful? 0
  • +
  • -

#27 TechnoBear   User is online

  • Lady A
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,149
  • Joined: 02-November 11

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

Posted 26 December 2018 - 06:38 AM

Sheepings, if you noticed in your listing there is a VBCrLf between each line, using their original data. Hence me telling them to split by VB.NewLine and then by the pipe, this would simplify your entire process.

What you have done in 100s of lines could be done in less than 10.
Was This Post Helpful? 0
  • +
  • -

#28 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,025
  • Joined: 05-December 13

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

Posted 26 December 2018 - 07:50 AM

That doesn't make much sense. You're admitting that you are splitting by each line at the line breaks, but then splitting again at each pipe. So do by all means; explain how your proposal is any different or in any way better when you are splitting more times than I am? Common maths would tell you that you are splitting at each line and then at each pipe, which is more splitting than at each pipe... think about it

Also, how the OP cyphers this data from whatever source they are using to get it, requires additional pipes at each relationship status, since their is no pipe, it will not insert into the DGV to the correct fields, and so, I think my method worked very well. If you re-read what i wrote; I advised fixing the problem of line breaks as the information is received to the string before it is added to it. Prevention is better than cure.

Also note that you have some vbCrLf from line breaking your string, and you can avoid that by putting your string on one line like this ::

I normally don't post support on anything DGV/DS related, as I am no expert on that control, nor do i want to be for something so dated. While there was probably another way to do it... I'm not seeing anyone else writing lengthy posts explaining how to do it in the detail I've gone into.

PS. Hope you are having a great Christmas. :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2