CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2012
    Posts
    3

    show/hide datagridview rows by setting trackbar value

    Hi.
    I need a help with showing/hiding rows in datagridview by setting the trackbar value. The below example works if there are not too much rows. If there are greater number of rows then after you change a value of trackbar you must wait for a few seconds for the datagridview to redraw and only then you can use trackbar again...
    Apparently I must use multithreading to achieve what I want: when I use trackbar, datagridview starts to redraw, but if I use trackbar again before datagridview finishes redrawing, it immediately stops redrawing according to previous trackbar value and starts to redraw from the beginning according to current trackbar value. That way trackbar will never be frozzen (stuck, unusable...).
    I tried with backgroundworker but got error "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on." - Maybe didn't use it right?

    P.S. I have old and slow computer. If you have new and fast computer maybe you wont experience what I'm describing here so try to increase number of added rows in line 'For i As Integer = 1 To 5000' (I added 5000 rows here) - and if you have even older and slower computer than you will have to decrease this value or you will have to wait to long after you drag the thumb in trackbar.



    Code:
    Public Class Form1
    
        WithEvents trackbar1 As New TrackBar
        WithEvents DataGridView1 As New DataGridView
        Dim label1 As New Label
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
            'trackbar
            Me.Controls.Add(trackbar1)
            trackbar1.Location = New Point(0, 150)
            trackbar1.Size = New Size(220, 140)
            trackbar1.Maximum = 40
            trackbar1.LargeChange = 1
            trackbar1.SmallChange = 1
            AddHandler trackbar1.Scroll, AddressOf TrackBar1_ScrollHandler
    
            'labels
            Me.Controls.Add(label1)
            label1.Location = New Point(0, 200)
            label1.Size = New Size(220, 140)
            label1.Text = "Only rows with values in Column2 greater than 0 are visible"
    
            'datagridview
            Me.Controls.Add(DataGridView1)
            DataGridView1.Location = New Point(0, 0)
            DataGridView1.Size = New Size(220, 140)
            DataGridView1.AllowUserToAddRows = False
            DataGridView1.RowHeadersVisible = False
            DataGridView1.TabIndex = 1
    
            'adding columns
            Dim column1 = New DataGridViewTextBoxColumn()
            column1.Name = "Column 1"
            column1.ValueType = GetType(System.DateTime)
            column1.ReadOnly = True
            DataGridView1.Columns.Add(column1)
            Dim column2 = New DataGridViewTextBoxColumn()
            column2.Name = "Column 2"
            column2.ValueType = GetType(System.Int64)
            column2.ReadOnly = True
            DataGridView1.Columns.Add(column2)
    
            'adding example data
            Dim rnd1 As New Random
            For i As Integer = 1 To 5000
                DataGridView1.Rows.Add(DateSerial(2011, rnd1.Next(1, 12), rnd1.Next(1, 28)), rnd1.Next(0, 40))
            Next
    
        End Sub
    
        Private Sub TrackBar1_ScrollHandler(sender As System.Object, e As System.EventArgs)
    
            For i As Long = 0 To (DataGridView1.Rows.Count - 1)
                If DataGridView1.Rows(i).Cells("Column 2").Value < trackbar1.Value Then
                    DataGridView1.Rows(i).Visible = False
                Else
                    DataGridView1.Rows(i).Visible = True
                End If
            Next
    
            label1.Text = "Only rows with values in Column2 greater than " + trackbar1.Value.ToString + " are visible"
    
        End Sub
    End Class
    Last edited by bezkintos; February 3rd, 2012 at 08:11 PM. Reason: formating

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: show/hide datagridview rows by setting trackbar value

    If you can't throttle the DB engine to 100 rows. Otherwise, do it like phone's do it. Load one screen at a time, and load the next screen when use scrolls
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Feb 2012
    Posts
    3

    Re: show/hide datagridview rows by setting trackbar value

    Hi dglienna!

    Thanks for reply.

    There is no DB behind this part of my project because datagridview is used to show data from a sort of a log files (unlimited number of files) and all the data must be loaded together (at the same time) because there are some calculations made during the filtering which uses the whole data collection.
    Last edited by bezkintos; February 3rd, 2012 at 08:11 PM.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: show/hide datagridview rows by setting trackbar value

    I would say a delay in processing the scroll bar would help a lot. If the users intention is to move it 5 places to the left the code currently would be checking every record 5 times. This would be very annoying for the user who may have to wait for it to complete each process before he could move another click. A small delay would allow it to check only once.

    Of course if you want to filter the values I would think a TextBox where the user could just type in the value they want would be a much better choice.

    On another note when you are looping through a grid doing updates to the display it will be much faster if you set the gird to not visible and/or not auto redraw while the operation is taking place as that elimanates the need to referesh the screen 1000s of times during the process.
    Last edited by DataMiser; February 3rd, 2012 at 06:29 PM.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Feb 2012
    Posts
    3

    Re: show/hide datagridview rows by setting trackbar value

    Hi DataMiser!

    Thank you for your time!

    I tried all your suggestions: 1. delay, 2. visible=false, 3. autorefresh off

    Just in case I didn't understood something right, I'll described what I have done:

    1. I have used Thread.Sleep(3000) to delay it for 3 seconds but it's not good because as soon as I slide the trackbar it waits for 3 seconds and then starts to refresh the datagridview, all that time (from the moment I moved the trackbar's slider) trackbar is frozen and unusable - and I would like to be able to change the trackbar value during datagridview refreshing time forcing the datagridview to starts refreshing according to new value of trackbar.
    2. I have made datagridview not visible before looping through a grid, and then again made it visible after looping finished - still the same problem (haven't measured time, maybe it is a little faster, but 9 seconds instead 10 is still 9 seconds user must wait for trackbar to unfreeze)
    3. With this one I'm not sure. I have used SuspendLayout() and ResumeLayout(). I have read here about those methods and it all sounded clear but testing those methods left me a little confused because after using SuspendLayout() method on some control I still could do all these things msdn link says that can't be done: change size, posiotion... (all that before using ResumeLayout() method). Anyway, I tried to implement those methods also to my project, but still didn't accomplish improvements...

    Last, I now about using textbox instead trackbar but that's my last choice. Somehow using trackbar would be more elegant in my project (by my opinion) so I would like to try with my approach first... There is even better solution that I'm aware off: I can use trackbar to adjust value and then use the apply button. But I have my reasons why I would like to avoid apply button and use trackbar in somewhat more dynamic way but I don't want to bother you with details...

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: show/hide datagridview rows by setting trackbar value

    Definitely do not use sleep.

    What I would do is use a timer and a flag var. The flag var would hold a time and the timer would check that var against the current time to see if x time has elasped. If so then it would update the grid if not then the update will wait for a futre cycle of the timer. Once the grid update has been ran then the timer would be disabled until the trackbar is moved again.

    In the scroll handler I would set the time flag to the current time and make sure the timer is enabled.

    The result should be that the grid will not update until the user stops moving the track bar.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured