4 Replies - 9475 Views - Last Post: 05 October 2012 - 08:00 AM Rate Topic: -----

#1 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

datagridview automatic select next row

Posted 05 October 2012 - 01:01 AM

i have a DGV and a timer, how can i select next row every 5 seconds and looping if last row reach?

my DGV enable adding = false

here is my code so far, it's work, but when reach last row it will stop :

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 5
            DataGridView1.Rows.Add("test")
        Next
        Timer1.Start()
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If DataGridView1.Rows.Count = 0 Then
        ElseIf DataGridView1.CurrentRow.Index = DataGridView1.Rows.Count - 1 Then
            DataGridView1.Rows(0).Selected = True
        Else
            Dim nextRow As DataGridViewRow = DataGridView1.Rows(DataGridView1.CurrentRow.Index + 1)
            DataGridView1.CurrentCell = nextRow.Cells(0)
            nextRow.Selected = True
        End If
    End Sub



This post has been edited by artemix22: 05 October 2012 - 01:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: datagridview automatic select next row

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: datagridview automatic select next row

Posted 05 October 2012 - 02:12 AM

You just need to rearrange your lines of code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim counter As Integer = DataGridView1.CurrentRow.Index + 1
        Dim nextRow As DataGridViewRow
        If counter = DataGridView1.RowCount Then
            nextRow = DataGridView1.Rows(0)
        Else
            nextRow = DataGridView1.Rows(counter)
        End If

        DataGridView1.CurrentCell = nextRow.Cells(0)
        nextRow.Selected = True

    End Sub


Was This Post Helpful? 1
  • +
  • -

#3 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: datagridview automatic select next row

Posted 05 October 2012 - 07:08 AM

thanks bro, it's work, i modify it for "zero row" :

If DataGridView1.Rows.Count = 0 Then
Else
  Dim counter As Integer = DataGridView1.CurrentRow.Index + 1
  Dim nextRow As DataGridViewRow
  If counter = DataGridView1.RowCount Then
     nextRow = DataGridView1.Rows(0)
  Else
     nextRow = DataGridView1.Rows(counter)
  End If
     DataGridView1.CurrentCell = nextRow.Cells(0)
     nextRow.Selected = True
End If


Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: datagridview automatic select next row

Posted 05 October 2012 - 07:33 AM

Yeah. Instead of If DataGridView1.Rows.Count = 0 Then Else, you can simply say: If DataGridView1.Rows.Count > 0 Then, and leave the Else out. Similarly, you can use <>. In my opinion, you should never start timer, if there's no rows to loop through in DGV, that's why I left out the 0 rows check.
Was This Post Helpful? 1
  • +
  • -

#5 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: datagridview automatic select next row

Posted 05 October 2012 - 08:00 AM

thanks again bro, your suggestion is nice.. and i change a little thing for decrease (-) :

If DataGridView1.Rows.Count > 0 Then
   Dim counter As Integer = DataGridView1.CurrentRow.Index - 1
   Dim nextRow As DataGridViewRow
   If counter < DataGridView1.Rows(0).Index Then
      nextRow = DataGridView1.Rows(DataGridView1.Rows.Count - 1)
   Else
      nextRow = DataGridView1.Rows(counter)
   End If
      DataGridView1.CurrentCell = nextRow.Cells(0)
      nextRow.Selected = True
End If

This post has been edited by artemix22: 05 October 2012 - 08:05 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1