CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  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

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