Get count of datagridview inside a thread-VBForums
Results 1 to 9 of 9

Thread: Get count of datagridview inside a thread

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2016
    Posts
    66

    Get count of datagridview inside a thread

    Update: It does not appear that I can access anything from the DataGridView from within my thread.... I am trying to just simply pull some information from a cell and I am getting this error: "Index was out of range. Must be non-negative and less than the size of the collection." Why is it not working with my DataGridView????

    I am trying to loop through my DataGridView using a thread, but it does not appear to be getting the proper count; therefor it is unable to loop through my DataGridView.

    Notes: My DataGridView is on a separate form and is being populated before the thread is executed. The thread is executed by a button on a separate form from the DataGridView.

    Here is the code from the form trying to run the thread: (The code in bold returns a value of -1)

    Code:
    Public thread_UpdateDGVCheckBox As Threading.Thread
    
    
    Private Sub ThreadedDGVCheckBoxUpdate()
    
            Dim TST As String
            Dim StringCnt As Integer
    
            StringCnt = DGV_Form.DataGridView1.RowCount - 1
            MsgBox(StringCnt)
    
            For counter As Int32 = 0 To DGV_Form.DataGridView1.RowCount - 1
                TST = DGV_Form.DataGridView1.Item(3, DGV_Form.DataGridView1.Rows(counter).Index).Value.ToString()
                If TST = "loc_132" Then
                    DGV_Form.Invoke(Sub()
                                            DGV_Form.DataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.Red
                                            DGV_Form.DataGridView1(0, DGV_Form.DataGridView1.Rows(counter).Index).Value = True
                                        End Sub)
                End If
            Next
        End Sub
    
    
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button11.Click
    
            thread_UpdateDGVCheckBox = New Threading.Thread(AddressOf ThreadedDGVCheckBoxUpdate)
            thread_UpdateDGVCheckBox.Start()
    
    End Sub
    Last edited by Christhemist; Nov 15th, 2016 at 04:19 PM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,539

    Re: Get count of datagridview inside a thread

    There are two issues here.

    1. You appear to be using the default instance of a form. Default instances are thread-specific. If you have an instance of DGV_Form displayed to the user then that form was created on the UI thread. If you then use the default instance of DGV_Form on a different thread then it will be a different object. That is presumably why you get an error: the grid you're accessing doesn't contain any data, unlike the grid you're looking at.

    2. Addressing point 1 will be of no benefit to you because you will then be trying to access a control on a thread other than the one that it was created on, which is illegal.

    In short, stop trying to access data in DataGridView on a secondary thread because it's not possible. You appear to have some understanding of cross-threading issues because you're calling Invoke but what use is that when you're doing so after you've already tried to get data out of the grid? Also, what's the point of having your loop in the secondary thread if you're just going to keep going back to the UI thread anyway?

    What I would suggest you do is start by binding your grid to a data source if it is not already. You can then access that data source on a secondary thread. Loop through the items in the data source and store the indexes of all the items that require a change to the grid. Once that background task is complete, you can then loop through the output indexes on the UI thread and make the appropriate changes to the grid. I would suggest using a BackgroundWorker for that, because you can do the background work in the DoWork event handler and then update the UI in the RunWorkerCompleted event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2016
    Posts
    66

    Re: Get count of datagridview inside a thread

    So, I believe I've made the necessary changes you recommended. Please see below:

    Code:
    Private Sub ThreadedDGVCheckBoxUpdate()
           
            For i As Int32 = DtSample.Rows.Count - 1 To 0 Step -1
    
                If Not IsDBNull(DtSample.Rows(i)("Selected")) AndAlso DtSample.Rows(i)("Selected") = True Then
                    DtSample.Rows.Remove(DtSample.Rows(i))
                End If
    
            Next
        End Sub

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,539

    Re: Get count of datagridview inside a thread

    Quote Originally Posted by Christhemist View Post
    So, I believe I've made the necessary changes you recommended. Please see below:

    Code:
    Private Sub ThreadedDGVCheckBoxUpdate()
           
            For i As Int32 = DtSample.Rows.Count - 1 To 0 Step -1
    
                If Not IsDBNull(DtSample.Rows(i)("Selected")) AndAlso DtSample.Rows(i)("Selected") = True Then
                    DtSample.Rows.Remove(DtSample.Rows(i))
                End If
    
            Next
        End Sub
    I assume that DtSample is a DataTable. If that DataTable is bound to the grid then you will probably still have cross-thread issues with that code. Any change to a bound DataTable is going to result in a change to the grid too, so if you make the change to the DataTable on a secondary thread then the change to the grid will occur on the secondary thread too. If you read my post again then you'll see that what I actually told you to do was to work out what changes needed to be made on the secondary thread and then to actually make them on the UI thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2016
    Posts
    66

    Re: Get count of datagridview inside a thread

    I would then ask what do you mean by the UI thread? Do you mean the DataGridView itself?

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,539

    Re: Get count of datagridview inside a thread

    No, I mean the UI thread, i.e. the thread on which all your UI elements were created. Unless you create a separate thread, everything you do will be done on the UI thread. If you create a new thread yourself then anything you do on that thread is not done on the UI thread. If you do as I suggested 24 hours ago and use a BackgroundWorker then it will all be made simple, which is the whole point of the BackgroundWorker class. You call RunWorkerAsync and it will raise its DoWork event on a secondary thread. You do your background work in the handler for that event and, when that completes, it raises its RunWorkerCompleted event on the UI thread. You can then safely update the UI in the handler for that event. If you need to update the UI along the way, you can call ReportProgress and handle the ProgressChanged event, which is also raised on the UI thread. If you follow the CodeBank link in my signature below, you'll find a thread dedicated to Using The BackgroundWorker.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2016
    Posts
    66

    Re: Get count of datagridview inside a thread

    Ok, well what you suggested is a bit over my head at the moment, I am somewhat new to programming, but I will definitely look into what you recommended I do. I got the concept in which you are explaining, I just have to research the syntax mostly.

    I basically just have a windows form that has a tool-strip along it that each item within the tool-strip does something to modify the datagridview on my form. I am still lost as to what my "UI thread" is....

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,539

    Re: Get count of datagridview inside a thread

    Quote Originally Posted by Christhemist View Post
    I got the concept in which you are explaining, I just have to research the syntax mostly.
    I guess that it's probably a good thing that I directed you to my existing examples in the CodeBank then.
    Quote Originally Posted by Christhemist View Post
    I am still lost as to what my "UI thread" is....
    Why did you create a new thread in your code if you don't even know what threads are? You can think of a thread a series of instructions executed by a processor core. Each core can only execute one set of instructions at a time. If you want your application to be able to do two things at the same time then you need two threads so that two cores can each execute one set of instructions. Your application has a set of instructions to execute so it must have one thread by default. That is often called the main thread or, for GUI apps, the UI thread because it's the thread on which all instructions relating to the UI are executed. If you want to write code that does anything to the UI then that code must be executed on that thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2016
    Posts
    66

    Re: Get count of datagridview inside a thread

    The code I provided was recommended to me in another post as a way to increase the speed in which I remove a large amount of records from a datagridview and/or datasource. Instructions on how to use it were not given. Here is a link to the post: http://www.vbforums.com/showthread.p...a-DataGridView

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.