Results 1 to 4 of 4

Thread: Datagrid Highlighting with Timer

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    21

    Datagrid Highlighting with Timer

    Hi Guys - I have a datagrid with N number of Rows, and I want to highlight each and every row for 5 seconds then move to the second row and then the third and so on until the last row.

    the code is working so that I can scroll down every 5 seconds but I cant highlight the row keep it highlighted for 5 seconds and then highlight the next one and so on.

    Here is the code so far, what am I doing wrong would be grateful if someone can please help me out.


    Code:
     Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
    
            Dim mySelectQuery As String = "SELECT para, jtext FROM rooter where (jID = '" & jID & "')  and mno between '" & FromA & "' and '" & ToA & "'"
            Dim sqConnection As New SQLiteConnection(path)
            Dim da As SQLiteDataAdapter = New System.Data.SQLite.SQLiteDataAdapter(mySelectQuery, sqConnection)
            Dim cb As System.Data.SQLite.SQLiteCommandBuilder = New System.Data.SQLite.SQLiteCommandBuilder(da)
            Dim dt As DataTable = New System.Data.DataTable
            Dim bindingSource1 As New BindingSource()
            DataGridView1.DataSource = BindingSource1
            da.Fill(dt)
            bindingSource1.DataSource = dt
    
            With DataGridView1
    
                .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
                .Font = New Font("ariel", 26, FontStyle.Regular)
                .ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
                .CellBorderStyle = DataGridViewCellBorderStyle.Single
                .GridColor = Color.Black
                .RowHeadersVisible = False
    
                .ColumnHeadersVisible = False
                Dim column As DataGridViewColumn = DataGridView1.Columns(1)
                .Columns("ayahno").Visible = False
                .Columns("ayahno").ReadOnly = True
                column.Width = 1112
                .DefaultCellStyle.WrapMode = DataGridViewTriState.True
                 .SelectionMode = DataGridViewSelectionMode.FullRowSelect
    
            End With
    
            Timer1.Interval = 5000
            Timer1.Enabled = True
            Timer1.Start()
    
    
    
            sqConnection.Close()
    
            sqConnection.Dispose()
        End Sub
    And then I have this in my timer_tick section


    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
            Dim rowcount = DataGridView1.RowCount
            For i As Integer = 0 To rowcount - 1
                DataGridView1.FirstDisplayedScrollingRowIndex = 0
                DataGridView1.CurrentCell = DataGridView1.Rows(i).Cells(1)
                DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
                DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Bisque
                DataGridView1.FirstDisplayedScrollingRowIndex = DataGridView1.FirstDisplayedScrollingRowIndex + 1
            Next
    
        End Sub

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Datagrid Highlighting with Timer

    KISS!

    vb.net Code:
    1. Dim i As Int16 = 0 ' this needs to be form level
    2.  
    3.  Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
    4. 'your data selection code
    5.  
    6.       Timer1.Interval = 5000
    7.         Timer1.Enabled = True 'no need to enable it and start it; one or t'other
    8.  
    9.         DataGridView1.ClearSelection() 'optional
    10.         DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'otherwise you have to wait 5 seconds for first one
    11.     End Sub
    12.  
    13.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    14.         If i < DataGridView1.Rows.Count - 1 Then 'stay within bounds
    15.             i = i + 1 ' for all rows except Row0
    16.  
    17.             DataGridView1.Rows(i - 1).DefaultCellStyle.BackColor = Color.White ' restore previous highlight
    18.             DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'new highlight
    19.         Else            'next row is off the bottom so
    20.             i = 0        'reset index
    21.             DataGridView1.Rows(DataGridView1.Rows.Count - 1).DefaultCellStyle.BackColor = Color.White 'restore bottom row
    22.             DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'highlight top row
    23.  
    24.         End If
    25.  
    26.  
    27.     End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    21

    Re: Datagrid Highlighting with Timer

    Thanks a heap dunfiddlin, you saved me god knows how many weeks of frustration, you are truly my hero. That was some amazing programming.

    Now I have my datagrid autoscrolling line by line and highlighting nicely based on my timer.

    There is one more thing if someone might be able to help with, I am trying to implement the mouse wheel line by line scrolling on my datagrid, I have tried the below code but somehow its fails to work.

    Code:
    Private Const SB_LINEDOWN As Integer = 1
    SendMessage(DataGridView1.Handle.ToInt32(), WM_VSCROLL, SB_LINEDOWN, 0)

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Datagrid Highlighting with Timer

    vb.net Code:
    1. Private Sub DataGridView1_MouseWheel(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseWheel
    2.         Dim linesToScroll = SystemInformation.MouseWheelScrollLines
    3.  
    4.         Select Case e.Delta
    5.             Case 120 'Scrolling up
    6.                 i = Math.Max(0, i - linesToScroll)
    7.             Case -120 'Scrolling down
    8.                 i = Math.Min(DataGridView1.Rows.Count - 1, i + linesToScroll)
    9.         End Select
    10.         DataGridView1.FirstDisplayedScrollingColumnIndex = i
    11.     End Sub

    Edit: Should also point out that you'll need to do the highlighting here as well. I'd suggest refactoring out that bit of code so both methods can use it.
    Last edited by MattP; Aug 22nd, 2012 at 03:06 PM.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

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
  •  



Click Here to Expand Forum to Full Width