(HorizontalScrollingOffset) loses its value. How to prevent (loss of value) In DataGridView .

Mansour_Dalir 1,371 Reputation points
2023-07-18T09:08:11.31+00:00

hi

It loses this value due to the lack of horizontal scrolling (dgv.ScrollBars =ScrollBars.Vertical). How can I prevent this? It causes noise. (noise \flicker). thank

Public Class frmHScroll
      'Objects dgv,dgvTotal As DataGridView

    Private Sub dgv_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles dgv.ColumnWidthChanged
        WidthsChange()
    End Sub
    Private Sub dgv_RowHeadersWidthChanged(sender As Object, e As EventArgs) Handles dgv.RowHeadersWidthChanged
        WidthsChange()
    End Sub
    Private Sub WidthsChange()
        dgvTotal.RowHeadersWidth = dgv.RowHeadersWidth
        dgv.Columns.Cast(Of DataGridViewColumn).ToList().ForEach(Sub(h) dgvTotal.Columns(h.Index).Width = h.Width)
        dgv.Columns.Cast(Of DataGridViewColumn).ToList().ForEach(Sub(h) dgvTotal.Columns(h.Index).Visible = h.Visible)
        dgv_Resize(Nothing, Nothing)
    End Sub
    Private Sub dgv_Resize(sender As Object, e As EventArgs) Handles dgv.Resize
        dgv.HorizontalScrollingOffset = dgvTotal.HorizontalScrollingOffset  'is Line Problem
        ' dgvTotal.Columns("ColOffset").Visible = dgv.Rows.Count > dgv.DisplayedRowCount(False)
    End Sub
    Private Sub dgv_ColumnAdded(sender As Object, e As DataGridViewColumnEventArgs) Handles dgv.ColumnAdded
        dgv.ScrollBars = ScrollBars.Vertical 'to Horizontal Scroll Hide Of Main dgv
    End Sub
    Private Sub dgvTotal_Scroll(sender As Object, e As ScrollEventArgs) Handles dgvTotal.Scroll
        dgv.HorizontalScrollingOffset = e.NewValue
    End Sub
End Class

20230718_133008

20230718_214529

Problem

dgv.HorizontalScrollingOffset = dgvTotal.HorizontalScrollingOffset 'is Line Problem

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,798 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,496 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 24,201 Reputation points Microsoft Vendor
    2023-07-19T09:49:46.49+00:00

    Hi @Mansour_Dalir , When I tested with the code below, I did find that dgv.HorizontalScrollingOffset was reset to 0 every time dgv.Resize was triggered. But that didn't seem to cause the problem.

        Private Sub dgv_Resize(sender As Object, e As EventArgs) Handles dgv.Resize
            Console.WriteLine(dgv.HorizontalScrollingOffset)
            dgv.HorizontalScrollingOffset = dgvTotal.HorizontalScrollingOffset  'is Line Problem
            Console.WriteLine(dgv.HorizontalScrollingOffset)
        End Sub
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful