INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Datagridview - prevent columns scrolling to right when changing rows

Datagridview - prevent columns scrolling to right when changing rows

Datagridview - prevent columns scrolling to right when changing rows

(OP)
Suppose you have clicked on column 1 or higher and that column is only partly displayed so some is hidden off to the right.
Now when you use the up/down arrows to change rows, that column scrolls/shifts over to the right to display it's right hand side.
This is not very pleasing and is also a nuisance if you have a primary key on the left column and wish to see this whenever changing rows.

The following code resets the row to show first column when you select a different row by arrow keys or clicking with the mouse.

CODE --> vb.net

Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
     DataGridView1.CurrentCell = DataGridView1.CurrentRow.Cells(0)
End Sub

Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
      DataGridView1.FirstDisplayedScrollingColumnIndex = 0
End Sub 

Scrolling vertically with the wheel mouse or scroll bar still leaves the horizontal scroll position where you put it; it's only when you select a different row that the columns shift to show the first column again.

Basically you need to set the current cell to cell 0 as you leave the row then set the first displayed column to column 0 as you enter the new row.
Both steps are required to prevent problems and prevent continuous visual disturbances with every row change (which occur if you let the column change and paint then simply change column again in the row enter)

This method gives a pleasing, smooth change with subsequent row changes.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close