locked
Show always horizontal and vertical scrollbar in a datagridview RRS feed

  • Question

  • How can i do it?

    I'm using the code below to show the vertical scrollbar permanently but when i play around for the location and the size of the horizontal scrollbar my visual studio crashes.

    Regards


    public MyGridView()
            {
                VerticalScrollBar.Visible = true;
    
                VerticalScrollBar.VisibleChanged += new EventHandler(ShowVScrollBars);
            }
    
            private void ShowVScrollBars(object sender, EventArgs e)
            {
                if (!VerticalScrollBar.Visible)
                {
                    int width = VerticalScrollBar.Width;
                    VerticalScrollBar.Location = new Point(ClientRectangle.Width - width - 1, 1);
                    VerticalScrollBar.Size = new Size(width, ClientRectangle.Height - 2);
                    VerticalScrollBar.Show();
                }
            }



    • Edited by ChrisHar Monday, November 16, 2020 5:16 PM
    Monday, November 16, 2020 5:13 PM

All replies

  • Hello,

    Have not tried this but you might

    http://iborn2code.blogspot.com/2009/04/c-custom-datagridview-class-with-always.html


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Monday, November 16, 2020 5:34 PM
  • Hi ChrisHar,

    Thank you for posting here.

    I reproduced your problem by this code:

        class MyDatagridview : DataGridView
        {
            public MyDatagridview()
            {
                VerticalScrollBar.Visible = true;
                HorizontalScrollBar.Visible = true;
    
                HorizontalScrollBar.VisibleChanged += HorizontalScrollBar_VisibleChanged;
                VerticalScrollBar.VisibleChanged += VerticalScrollBar_VisibleChanged;
    
            }
    
            private void VerticalScrollBar_VisibleChanged(object sender, EventArgs e)
            {
                if (!VerticalScrollBar.Visible)
                {
                    int width = VerticalScrollBar.Width;
                    VerticalScrollBar.Location = new Point(ClientRectangle.Width - width - 1, 1);
                    VerticalScrollBar.Size = new Size(width, ClientRectangle.Height-2);
                    VerticalScrollBar.Show();
                }
            }
    
            private void HorizontalScrollBar_VisibleChanged(object sender, EventArgs e)
            {
                if (!HorizontalScrollBar.Visible)
                {
                    int height = HorizontalScrollBar.Height;
                    HorizontalScrollBar.Location = new Point(1, ClientRectangle.Height - height );
                    HorizontalScrollBar.Size = new Size(ClientRectangle.Width - 2-17,height);
                    HorizontalScrollBar.Show();
                }
            }
        }

    If I make any changes to it and then run the project, Visual Studio will definitely crash, but after opening Visual Studio again, it won’t crash anymore.

    It is always impossible to operate it in the designer, whether it is moving or deleting, but as long as the relevant code of the horizontal scroll bar is commented, it can be operated normally.

    This is not a solution to this problem, but a solution for the current situation where this control cannot be operated in the designer.

    I am still looking for how to add a horizontal scroll bar to the datagridview, if there are new discoveries, I will tell you here.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 17, 2020 7:15 AM
  • Hi Timon,

    Thanks for your support.

    The code below works without any crashes.

    For completeness:

    1. One of the scrollbars fills the corner unwanted when it's visible.

    2. I need to fill the corner with the same color of the scrollbar (shouldn't be empty) but it has to move when resizing and it has to cover the background/ gridcells.

    Any ideas?

    Regards

    public XGridView() {
                // make scrollbars visible & add handler
                try
                {
                    VerticalScrollBar.Visible = true;
                    HorizontalScrollBar.Visible = true;
                }
                catch
                {
                }
     
                VerticalScrollBar.VisibleChanged += ShowVScrollBar;
                HorizontalScrollBar.VisibleChanged += ShowHScrollBar;
            }
     
            private void ShowVScrollBar(object senderEventArgs e)
            {
                try
                {
                    if (!VerticalScrollBar.Visible)
                    {
                        int width = VerticalScrollBar.Width;
                        VerticalScrollBar.Location = new Point(ClientRectangle.Width - width -11);
                        VerticalScrollBar.Size = new Size(width, ClientRectangle.Height - 19 );
                        VerticalScrollBar.Show();
                    }
                }
                catch
                {
                }
            }
     
            private void ShowHScrollBar(object senderEventArgs e)
            {
                try
                {
                    if (!HorizontalScrollBar.Visible)
                    {
                        int height = HorizontalScrollBar.Height;
                        HorizontalScrollBar.Location = new Point(1, ClientRectangle.Height - height - 1);
                        HorizontalScrollBar.Size = new Size(ClientRectangle.Width - 19height);
                        HorizontalScrollBar.Show();
                    }
                }
                catch
                {
                }
            }


    • Edited by ChrisHar Wednesday, November 18, 2020 3:58 PM
    Wednesday, November 18, 2020 3:56 PM
  • Oh, i was wrong. It still crashes.
    Wednesday, November 18, 2020 4:13 PM
  • Hi ChrisHar,

    I suggest you go to the Developer Community to report a problem.

    Let the Visual Studio team investigate whether there are some potential problems that cause Visual Studio to crash.

    Best Regards,

    Timon


    "Visual c#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post.

    Thursday, November 19, 2020 7:51 AM
  • Hi Timon,

    Ok, didn't think that it could be a VS related issue. I thought it's a code problem.

    I will report it as you suggested.

    Regards.

    Friday, November 20, 2020 9:42 AM