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
Re: Datagrid Highlighting with Timer
KISS!
vb.net Code:
Dim i As Int16 = 0 ' this needs to be form level
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
'your data selection code
Timer1.Interval = 5000
Timer1.Enabled = True 'no need to enable it and start it; one or t'other
DataGridView1.ClearSelection() 'optional
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'otherwise you have to wait 5 seconds for first one
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If i < DataGridView1.Rows.Count - 1 Then 'stay within bounds
i = i + 1 ' for all rows except Row0
DataGridView1.Rows(i - 1).DefaultCellStyle.BackColor = Color.White ' restore previous highlight
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'new highlight
Else 'next row is off the bottom so
i = 0 'reset index
DataGridView1.Rows(DataGridView1.Rows.Count - 1).DefaultCellStyle.BackColor = Color.White 'restore bottom row
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Bisque 'highlight top row
End If
End Sub
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)
Re: Datagrid Highlighting with Timer
vb.net Code:
Private Sub DataGridView1_MouseWheel(sender As Object, e As MouseEventArgs) Handles DataGridView1.MouseWheel
Dim linesToScroll = SystemInformation.MouseWheelScrollLines
Select Case e.Delta
Case 120 'Scrolling up
i = Math.Max(0, i - linesToScroll)
Case -120 'Scrolling down
i = Math.Min(DataGridView1.Rows.Count - 1, i + linesToScroll)
End Select
DataGridView1.FirstDisplayedScrollingColumnIndex = i
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.