-
Nov 10th, 2016, 03:54 PM
#1
Thread Starter
Lively Member
Is there a faster way to loop through large amounts of Rows in a DataGridView?
I am looping though about 100,000 records and checking a DataGridViewCheckBox Column, but it is taking way too long. Is there a faster way of doing this? Here is how I am currently doing it:
Code:
For Each R0w As DataGridViewRow In DataGridView1.Rows
Dim TST As String = DataGridView1.Item(3, R0w.Index).Value.ToString
If TST = "loc_132" Then
DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red
DataGridView1(0, R0w.Index).Value = True
End If
Next
-
Nov 10th, 2016, 04:00 PM
#2
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
Here are a few optimization tips:
- For/Next is faster than For/Each
- Declare your TST outside of the loop so you only create it once
- Declare the value that you're searching outside the loop so you only create it once
Code:
Dim TST AS String
Dim loc_123 As String = "loc_123"
For counter As Integer = 0 To DataGridView1.Rows.Count - 1
TST = DataGridView1.Item(3, counter).Value.ToString()
If TST = loc_123 Then
DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red
DataGridView1(0, counter).Value = True
End If
Next
However, if you're looping through that many rows you'll likely need to do this in a separate thread.
-
Nov 10th, 2016, 04:06 PM
#3
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
What do you mean by "However, if you're looping through that many rows you'll likely need to do this in a separate thread. "
-
Nov 10th, 2016, 04:29 PM
#4
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
Multithreading is a concept where you do a task on a separate thread other than the user interface thread. The reason why you would do this is because if you have a long running task then it will take up too much resources to where your UI will appear to freeze.
Here is an MSDN article on how to multithread within Visual Basic .NET: https://msdn.microsoft.com/en-us/lib...(v=vs.71).aspx
Here is a sample(free-typed) code for your scenario:
Code:
Private thread_UpdateDGVCheckBox As Threading.Thread
Private Sub ThreadedDGVCheckBoxUpdate()
Dim TST AS String
Dim loc_123 As String = "loc_123"
For counter As Integer = 0 To DataGridView1.Rows.Count - 1
TST = DataGridView1.Item(3, counter).Value.ToString()
If TST = loc_123 Then
Me.Invoke(Sub()
DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red
DataGridView1(0, counter).Value = True
End Sub
End If
Next
End Sub
Then when to use the code, you'd call:
Code:
thread_UpdateDGVCheckBox = New Threading.Thread(AddressOf ThreadedDGVCheckBoxUpdate)
thread_UpdateDGVCheckBox.Start()
-
Nov 10th, 2016, 04:34 PM
#5
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
Thank you for the helpful information!!! I will let you know how it goes once I give it a shot, because yeah after I made the changes of your initial suggestions it was still pretty slow. So, I will trying the multithreading!
-
Nov 10th, 2016, 04:51 PM
#6
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
No problem. Keep me updated on how you are doing.
Before I tip my hat for a while I wanted to make a point about this chunk of code:
Code:
Me.Invoke(Sub()
DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red
DataGridView1(0, counter).Value = True
End Sub
Using a Sub without a name like this is call an anonymous delegate. This will only work if you are using Visual Studios 2010 or higher; if you're using an older version like 2008 then you will need to use an old school delegate.
Here is a MSDN article on older style delegates just in case you need it: https://msdn.microsoft.com/en-us/lib...(v=vs.90).aspx
-
Nov 10th, 2016, 04:51 PM
#7
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
Whelp Im stepping through the code and its just immediately skipping this part of the code:
Code:
TST = DataGridView1.Item(3, DataGridView1.Rows(counter).Index).Value.ToString()
If TST = "loc_132" Then
Me.Invoke(Sub()
DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red
DataGridView1(0, DataGridView1.Rows(counter).Index).Value = True
End Sub)
End If
-
Nov 10th, 2016, 04:58 PM
#8
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
When you setup the breakpoint on your For/Next loop, what is the value of DataGridView1.Rows.Count - 1?
-
Nov 10th, 2016, 05:00 PM
#9
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
"0" from what I can tell...
-
Nov 10th, 2016, 05:06 PM
#10
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
just did some more digging it might be "-1"
-
Nov 10th, 2016, 05:06 PM
#11
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
Then that is why it is skipping the code, if there are no rows to loop through then there is no need to execute the code.
Edit - We cross-posted. If you pull up the watch window and type in:
Code:
DataGridView1.Rows.Count
It will tell you the amount.
-
Nov 10th, 2016, 05:08 PM
#12
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
Just to clarify I am accessing a datagridview on another form, not sure if this would have an affect on doing this? So, I've altered your code to look like this: (this MsgBox returns the value "-1" btw)
Code:
Dim StringCnt As Integer
StringCnt = MyFormName.DataGridView1.RowCount - 1
MsgBox(StringCnt)
-
Nov 10th, 2016, 05:13 PM
#13
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
The only way that it would affect the code is if the Form was never launched and so the data was never populated in the DataGridView.
-
Nov 10th, 2016, 05:15 PM
#14
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
That's weird its not seeing any rows, because the DataGridView is definitely populated...
-
Nov 10th, 2016, 05:18 PM
#15
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
Getting this from the watch window "Reference to a non-shared member requires an object reference" when I enter in:
Code:
DataGridView1.Rows.Count
Last edited by Christhemist; Nov 10th, 2016 at 05:30 PM.
-
Nov 10th, 2016, 05:31 PM
#16
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
If it's not even going into the For Loop then that not what making your app run slow. Where is this code located? What other code is relevant to what your doing.
-
Nov 10th, 2016, 05:38 PM
#17
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
@wes4dbt Its not getting a proper row count because of that error...
Code:
Private thread_UpdateDGVCheckBox As Threading.Thread
Private Sub ThreadedDGVCheckBoxUpdate()
Dim TST As String
Dim StringCnt As Integer
StringCnt = DataGridView1.RowCount - 1
MsgBox(StringCnt)
For counter As Int32 = 0 To DataGridView1.RowCount - 1
TST = DataGridView1.Item(3, DataGridView1.Rows(counter).Index).Value.ToString()
If TST = "loc_132" Then
Invoke(Sub()
DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red
DataGridView1(0, DataGridView1.Rows(counter).Index).Value = True
End Sub)
End If
Next
End Sub
-
Nov 10th, 2016, 05:47 PM
#18
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
Ok, where uis this "Private Sub ThreadedDGVCheckBoxUpdate()" called from?
And WHAT error? You haven't mentioned getting an error when you run your code.
-
Nov 10th, 2016, 05:49 PM
#19
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
It's being called from a button on a separate form.
Not an error but the output in the watch window reads "Reference to a non-shared member requires an object reference" when I input this code:
Code:
DataGridView1.Rows.Count
-
Nov 10th, 2016, 05:55 PM
#20
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
There's no way your call "Private Sub ThreadedDGVCheckBoxUpdate()" from a button on a seperate form. You have declared the Sub as Private, it would not be visible from another form. I think there is some information you have not provided that making solving this issue a lot harder than is should be. Always provide a complete explanation and all relevant code.
-
Nov 10th, 2016, 06:00 PM
#21
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
My bad, it is being called from a button on the same form using this:
Code:
thread_UpdateDGVCheckBox = New Threading.Thread(AddressOf ThreadedDGVCheckBoxUpdate)
thread_UpdateDGVCheckBox.Start()
Note: It's the DataGridView that is on another form, I have just excluded this from the code. So, its really:
Code:
MyFormName.DataGridView1.Rows.Count
Last edited by Christhemist; Nov 10th, 2016 at 06:04 PM.
-
Nov 10th, 2016, 06:13 PM
#22
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
thread_UpdateDGVCheckBox = New Threading.Thread(AddressOf ThreadedDGVCheckBoxUpdate)
thread_UpdateDGVCheckBox.Start()
Just providing this little snippet isn't enough, we have no idea where this is located.
The fact that the dgv is located on a different form is extremely important. Like I said before, please provide a complete explanation. Maybe someone else can help you. I'm done for now.
-
Nov 10th, 2016, 06:47 PM
#23
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
This snippet is on the same form in a Button_Click event
-
Nov 14th, 2016, 02:20 PM
#24
Thread Starter
Lively Member
Re: Is there a faster way to loop through large amounts of Rows in a DataGridView?
This issue is still unresolved if anyone else has anymore insight...
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|