none
Scrolling a datagridview

    Question

  • i need to scroll a datagridview, my scroll bar is set to false so i need to do it using code .
    currently i am scrolling using the following code, but i would like to modify the code to scroll from one line to the next and see the blue bar actually selecting the rows. how do i do so ?

     Private Sub grdMouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

            Dim currentIndex As Integer = Me.grdAccountTypes.FirstDisplayedScrollingRowIndex
            Dim scrollLines As Integer = SystemInformation.MouseWheelScrollLines

              Select e.Delta
                Case 120 'Scrolling up
                    ' We cannot scroll up past row 0.
                    Me.grdAccountTypes.FirstDisplayedScrollingRowIndex = Math.Max(0, currentIndex - scrollLines)
                Case -120 'Scrolling down
                    Me.grdAccountTypes.FirstDisplayedScrollingRowIndex = currentIndex + scrollLines
            End Select

        End Sub

    Saturday, October 23, 2010 8:58 PM

Answers

  • Hello EladRez,

    Based on my understanding you want to scroll one row ( to the next row) at one time by selecting the next whole row, if I'm off base, please let me know.

    If so, you should first set the SelectionMode property of the DataGridView to FullRowSelect.

    For you first question that "1. the procedure does scroll one row at a time, but it doesn't do so sequantialy after scrolling down, i need to scroll up once to scroll down again." you just need to put a check statement if the row index comes to the number of rows in the dataGridView( that means the last row), if so, put the selection to the first row. See code below.

    For you second question that "2. as you said there are no boundries set for the scroll, scrolling above the 0 row (first row) and below the last row, results in a crash." you need also a check statement to see if the row number is between the legal boundry. See code below.

    That is to say, all you need is put the checking statement in the Scroll event of the DataGridView as Steven Schultz suggested, you can add other requirements to your specific need,  here I'm just give you some checking examples as below 

    Private Sub DataGridView1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll

            ' MessageBox.Show(DataGridView1.Rows.Count().ToString())

            Dim i As Integer = DataGridView1.CurrentRow.Index + e.NewValue

            If DataGridView1.Rows.Count = 0 Then              'If no row existed right now

                MessageBox.Show("No row existed right now. Please fill in with records.")

     

            ElseIf i = DataGridView1.Rows.Count() - 1 Then  'If scroll to the last row, then put the selection to the first row

                DataGridView1(0, 0).Selected = True

     

            Else

                DataGridView1(0, i).Selected = True

            End If

        End Sub

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            DataGridView1.Rows.Add(1, 2, 3, 4)

            DataGridView1.Rows.Add(10, 9, 8, 7)

            DataGridView1.Rows.Add(4, 14, 16, 77)

            DataGridView1.Rows.Add(21, 14, 16, 77)

            DataGridView1.Rows.Add(53, 14, 16, 77)

            DataGridView1.Rows.Add(7, 14, 16, 77)

            DataGridView1.Rows.Add(39, 12, 16, 77)

            DataGridView1.Rows.Add(40, 44, 16, 77)

            DataGridView1.Rows.Add(82, 11, 66, 77)

            DataGridView1.Rows.Add(26, 14, 16, 77)

            DataGridView1.Rows.Add(58, 17, 16, 77)

            DataGridView1.AllowUserToAddRows = False

        End Sub

    End Class

     

    Next time please try your effort to do the coding other than asking people to do your work for you, you need to try to do it yourself since forum member already gave you the right logic  suggestion.

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by EladRez Tuesday, October 26, 2010 2:41 PM
    Tuesday, October 26, 2010 8:11 AM

All replies

  •     private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
          int i = dataGridView1.CurrentRow.Index + e.NewValue;
          dataGridView1[0, i].Selected = true;
        }
    Something like this should work... Keep in mind this is a basic example and provides no bounds checking, etc etc.. (also, works best if DGV Multiselect is false)
    Sunday, October 24, 2010 10:21 AM
  • Steven, thank you for you reply

    i am a newbie and kind of got lost with the procedure and do need some further help.

    here are some of the issues i have:

    1. the procedure does scroll one row at a time, but it doesn't do so sequantialy after scrolling down, i need to scroll up once to scroll down again.

    2. as you said there are no boundries set for the scroll, scrolling above the 0 row (first row) and below the last row, results in a crash.

    i know i should combain both procedures but just don't know how exactly. here is what i came with so far:

     

     

    If (RowIndex = (grdAccountTypes.Rows.Count)) Then

    grdAccountTypes.Rows(grdAccountTypes.CurrentRow.Index + 1).Selected =

     

    False

     

     

    Else

     

     

    If ((RowIndex) < (grdAccountTypes.Rows.Count)) Then

    grdAccountTypes.Rows(grdAccountTypes.CurrentRow.Index + 1).Selected =

     

    True

     

     

    End If

     

     

    End If

    i am using VB and not C

     

    Sunday, October 24, 2010 9:24 PM
  • Hello EladRez,

    Based on my understanding you want to scroll one row ( to the next row) at one time by selecting the next whole row, if I'm off base, please let me know.

    If so, you should first set the SelectionMode property of the DataGridView to FullRowSelect.

    For you first question that "1. the procedure does scroll one row at a time, but it doesn't do so sequantialy after scrolling down, i need to scroll up once to scroll down again." you just need to put a check statement if the row index comes to the number of rows in the dataGridView( that means the last row), if so, put the selection to the first row. See code below.

    For you second question that "2. as you said there are no boundries set for the scroll, scrolling above the 0 row (first row) and below the last row, results in a crash." you need also a check statement to see if the row number is between the legal boundry. See code below.

    That is to say, all you need is put the checking statement in the Scroll event of the DataGridView as Steven Schultz suggested, you can add other requirements to your specific need,  here I'm just give you some checking examples as below 

    Private Sub DataGridView1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll

            ' MessageBox.Show(DataGridView1.Rows.Count().ToString())

            Dim i As Integer = DataGridView1.CurrentRow.Index + e.NewValue

            If DataGridView1.Rows.Count = 0 Then              'If no row existed right now

                MessageBox.Show("No row existed right now. Please fill in with records.")

     

            ElseIf i = DataGridView1.Rows.Count() - 1 Then  'If scroll to the last row, then put the selection to the first row

                DataGridView1(0, 0).Selected = True

     

            Else

                DataGridView1(0, i).Selected = True

            End If

        End Sub

     

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            DataGridView1.Rows.Add(1, 2, 3, 4)

            DataGridView1.Rows.Add(10, 9, 8, 7)

            DataGridView1.Rows.Add(4, 14, 16, 77)

            DataGridView1.Rows.Add(21, 14, 16, 77)

            DataGridView1.Rows.Add(53, 14, 16, 77)

            DataGridView1.Rows.Add(7, 14, 16, 77)

            DataGridView1.Rows.Add(39, 12, 16, 77)

            DataGridView1.Rows.Add(40, 44, 16, 77)

            DataGridView1.Rows.Add(82, 11, 66, 77)

            DataGridView1.Rows.Add(26, 14, 16, 77)

            DataGridView1.Rows.Add(58, 17, 16, 77)

            DataGridView1.AllowUserToAddRows = False

        End Sub

    End Class

     

    Next time please try your effort to do the coding other than asking people to do your work for you, you need to try to do it yourself since forum member already gave you the right logic  suggestion.

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by EladRez Tuesday, October 26, 2010 2:41 PM
    Tuesday, October 26, 2010 8:11 AM