Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi how can i speed up my codes when writing data richtextbox to datagridview

i have 35000 rows in richtextbox

i tested my codes ;
but only 1000 rows write 1 minute

so when all data writing finişhed pasted 35minutes

What I have tried:

With STOK_LIST

          .Rows.Clear()
          For x = 0 To RichTextBox1.Lines.Count - 1
              Dim tx = RichTextBox1.Lines(x).ToString
              If tx <> "" Then
                  Dim i = STOK_LIST.Rows.Add
                  Label1.Text = +i
                  .Rows(i).Cells(0).Value = tx.Split(vbTab)(0)
                  .Rows(i).Cells(1).Value = tx.Split(vbTab)(1)
                  .Rows(i).Cells(2).Value = tx.Split(vbTab)(2)
                  .Rows(i).Cells(3).Value = tx.Split(vbTab)(3)
                  .Rows(i).Cells(4).Value = tx.Split(vbTab)(4)
                  .Rows(i).Cells(5).Value = tx.Split(vbTab)(5)
                  .Rows(i).Cells(6).Value = tx.Split(vbTab)(6)
                  .Rows(i).Cells(7).Value = tx.Split(vbTab)(7)
                  .Rows(i).Cells(8).Value = tx.Split(vbTab)(8)
                  .Rows(i).Cells(9).Value = tx.Split(vbTab)(9)
                  .Rows(i).Cells(10).Value = tx.Split(vbTab)(10)

              End If
          Next

      End With
Posted
Updated 18-Jun-20 2:49am
Comments
[no name] 18-Jun-20 3:40am    
First thing I would try is to split the source string _one_ time and use the one result to assign cell 0 to 10.

Here also some hints concerning BeginUpdate ... .net - BeginUpdate / EndUpdate for DataGridView request - Stack Overflow[^]
Member 14588284 18-Jun-20 3:58am    
ı cant open Stack Overflow site for our company proxy rules
can u help prepare an excample code
[no name] 18-Jun-20 6:58am    
Btw. DataGridView is not a good choice to save data. I would expect filling in your data first into a DataTable and bind the DataTable afterwards to the DGV will be more performant.
[no name] 18-Jun-20 7:32am    
I just made a small test in c#.
a.) Filling a RichtTextBox with 35'000 strings
b.) Loop over the RichTextBox-Lines and only access line by line without further processing.

And it takes time.... looks like 30Min. For me it looks like the RichTextBox is the bottleneck.

From where the data for the RichTextBox come originally? From a file? If yes, I suggest to bypass the RichTextBox.
Member 14588284 18-Jun-20 7:40am    
i'm copying rows from excel file then paste in richtextbox
then i want fill datagridview tables from richbox lines
i use this way because i didnt find anything pasting direct datagridview


Start by doing the split once per loop, not 11 - Split does a lot of object allocation so it's not fast.

Second, call Control.SuspendLayout Method (System.Windows.Forms) | Microsoft Docs[^] on your DGV before you start to add any rows, and then call Control.ResumeLayout Method (System.Windows.Forms) | Microsoft Docs[^] on it after your loop ends.

That should fix it.
 
Share this answer
 
Comments
Garth J Lancaster 18-Jun-20 3:44am    
+5, nice
Member 14588284 18-Jun-20 3:50am    
Start by doing the split once per loop, not 11 - Split does a lot of object allocation so it's not fast.

how can i do this?
OriginalGriff 18-Jun-20 4:23am    
Call it one time at the top of the loop, and use the array it returns instead of calling it repeatedly ...
Member 14588284 18-Jun-20 4:30am    
i didnt understand :(
OriginalGriff 18-Jun-20 4:46am    
What part do you not understand?
I'm not trying to be annoying, or dense - I only get exactly what you type to work with, I get no other context.
Please also take into account the suggestions from Solution 1.

Furthermore I think instead of accessing the lines of the richtextbox you should get them first something like this:
Dim textLines As List(Of String) = RichTextBox1.Lines.ToList()
and proceed your processing with textLines

Of course you can optimize much more. You can skip the RichTextBox by implementing Paste From Clipboard directly. Also using a DataTable as data store instead of DGV can lead to improvments and is also more clean.

I hope it helps and again, please take into acount, I don't know VB.NET so pay attention with the code snippet above.


[Edit 1] Forgive me that I don't know VB.NET. Anyway I tried it with it

Takes about 15 Min for 35'000 lines (i7/ 3.4 GHz):
VB
Private Sub ButtonProcessByRTBLines_Click(sender As Object, e As EventArgs) Handles ButtonProcessByRTBLines.Click

  Dim len = RichTextBox1.Lines.Count
  For i = 0 To len - 1
      Dim ln = RichTextBox1.Lines(i)
      If i Mod 500 = 0 Then
          Label1.Text = i.ToString()
          Application.DoEvents()
      End If
  Next
End Sub

Takes less than 0.5 seconds for 35'000 lines:
VB
Private Sub ButtonProcessByStringList_Click(sender As Object, e As EventArgs) Handles ButtonProcessByStringList.Click
  Dim textLines As List(Of String) = RichTextBox1.Lines.ToList()
  Dim len = textLines.Count

  For i = 0 To len - 1
      Dim ln = textLines(i)
      If i Mod 500 = 0 Then
          Label1.Text = i.ToString()
          Application.DoEvents()
      End If
  Next
End Sub


[Edit 2] Finally this not optimized code will be processed in less than 15 seconds. 35'000 lines on a i7/ 3.4 GHz:

Private Sub ButtonProcessToDGV_Click(sender As Object, e As EventArgs) Handles ButtonProcessToDGV.Click
    Dim textLines As List(Of String) = RichTextBox1.Lines.ToList()
    Dim textLinesCount = textLines.Count
    With STOK_LIST
        .Rows.Clear()
        For x = 0 To textLinesCount - 1
            Dim tx = textLines(x).ToString
            If tx <> "" Then
                Dim splittedLn = textLines(x).Split(vbTab)
                Dim i = STOK_LIST.Rows.Add
                Label1.Text = +i
                .Rows(i).Cells(0).Value = splittedLn(0)
                .Rows(i).Cells(1).Value = splittedLn(1)
                .Rows(i).Cells(2).Value = splittedLn(2)
                .Rows(i).Cells(3).Value = splittedLn(3)
                .Rows(i).Cells(4).Value = splittedLn(4)
                .Rows(i).Cells(5).Value = splittedLn(5)
                .Rows(i).Cells(6).Value = splittedLn(6)
                .Rows(i).Cells(7).Value = splittedLn(7)
                .Rows(i).Cells(8).Value = splittedLn(8)
                .Rows(i).Cells(9).Value = splittedLn(9)
                .Rows(i).Cells(10).Value = splittedLn(10)

            End If
            If x Mod 500 = 0 Then
                Label1.Text = x.ToString()
                Application.DoEvents()
            End If
        Next

    End With
End Sub
 
Share this answer
 
v5
Comments
Member 14588284 18-Jun-20 9:26am    
thanks a lot for all again.
[no name] 19-Jun-20 5:55am    
Did you notice my last edit in the answer? I think it will solve your request.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900