Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

Windows Forms DataGridView のスクロールバーを常時表示する

0
Posted at

はじめに

DataGridView の ScrollBars プロパティで Both を指定しても、内容が少ない場合はスクロールバー自体が非表示になるので、小細工で常時表示させる備忘録です。

改訂履歴

  • 2026/08/21 : 初版公開。

本文

1. 環境

  • .NET Framework 4.8.1
  • Visual Studio Community 2022

2. 実装

見た目はこんな感じになりました。

DataGridView を継承して作っています。

ExtendedDataGridView.cs
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace DataGridViewExample
{
    /// <summary>
    /// 拡張された <see cref="DataGridView"/> です。
    /// </summary>
    public class ExtendedDataGridView : DataGridView
    {
        private bool _showAlwaysHorizontalScrollBar = false;
        private bool _showAlwaysVerticalScrollBar = false;

        /// <summary>
        /// <see cref="ExtendedDataGridView"/> クラスの新しいインスタンスを初期化します。
        /// </summary>
        public ExtendedDataGridView()
        {
            DoubleBuffered = true;
        }

        /// <summary>
        /// 水平スクロールバーを常時表示するかどうかを示す値を取得または設定します。
        /// </summary>
        [Browsable(true)]
        [Category("拡張プロパティ")]
        [DefaultValue(false)]
        [Description("水平スクロールバーを常時表示するかどうかを示します。")]
        public bool ShowAlwaysHorizontalScrollBar
        {
            get
            {
                return _showAlwaysHorizontalScrollBar;
            }
            set
            {
                if (_showAlwaysHorizontalScrollBar == value)
                {
                    return;
                }

                _showAlwaysHorizontalScrollBar = value;

                if (_showAlwaysHorizontalScrollBar)
                {
                    HorizontalScrollBar.VisibleChanged -= HorizontalScrollBar_VisibleChanged;
                    HorizontalScrollBar.VisibleChanged += HorizontalScrollBar_VisibleChanged;
                    ShowHorizontalScrollBar();
                }
                else
                {
                    HorizontalScrollBar.VisibleChanged -= HorizontalScrollBar_VisibleChanged;
                    HorizontalScrollBar.Visible = false;
                }
            }
        }

        /// <summary>
        /// 垂直スクロールバーを常時表示するかどうかを示す値を取得または設定します。
        /// </summary>
        [Browsable(true)]
        [Category("拡張プロパティ")]
        [DefaultValue(false)]
        [Description("垂直スクロールバーを常時表示するかどうかを示します。")]
        public bool ShowAlwaysVerticalScrollBar
        {
            get
            {
                return _showAlwaysVerticalScrollBar;
            }
            set
            {
                if (_showAlwaysVerticalScrollBar == value)
                {
                    return;
                }

                _showAlwaysVerticalScrollBar = value;

                if (_showAlwaysVerticalScrollBar)
                {
                    VerticalScrollBar.VisibleChanged -= VerticalScrollBar_VisibleChanged;
                    VerticalScrollBar.VisibleChanged += VerticalScrollBar_VisibleChanged;
                    ShowVerticalScrollBar();
                }
                else
                {
                    VerticalScrollBar.VisibleChanged -= VerticalScrollBar_VisibleChanged;
                    VerticalScrollBar.Visible = false;
                }
            }
        }

        private void HorizontalScrollBar_VisibleChanged(object sender, EventArgs e)
        {
            if (_showAlwaysHorizontalScrollBar && !HorizontalScrollBar.Visible)
            {
                HorizontalScrollBar.VisibleChanged -= HorizontalScrollBar_VisibleChanged;
                ShowHorizontalScrollBar();
                HorizontalScrollBar.VisibleChanged += HorizontalScrollBar_VisibleChanged;
            }
        }

        private void VerticalScrollBar_VisibleChanged(object sender, EventArgs e)
        {
            if (_showAlwaysVerticalScrollBar && !VerticalScrollBar.Visible)
            {
                VerticalScrollBar.VisibleChanged -= VerticalScrollBar_VisibleChanged;
                ShowVerticalScrollBar();
                VerticalScrollBar.VisibleChanged += VerticalScrollBar_VisibleChanged;
            }
        }

        private void ShowHorizontalScrollBar()
        {
            if (DesignMode)
            {
                return;
            }

            int height = HorizontalScrollBar.Height;

            // 垂直スクロールバー表示中は、その分だけずらして描画する。
            Size size = _showAlwaysVerticalScrollBar
                ? new Size(ClientSize.Width - VerticalScrollBar.Width - 2, height)
                : new Size(ClientSize.Width - 2, height);

            HorizontalScrollBar.Location = new Point(1, ClientSize.Height - height - 1);
            HorizontalScrollBar.Size = size;
            HorizontalScrollBar.Show();
        }

        private void ShowVerticalScrollBar()
        {
            if (DesignMode)
            {
                return;
            }

            int width = VerticalScrollBar.Width;
            VerticalScrollBar.Location = new Point(ClientSize.Width - width - 1, 1);
            VerticalScrollBar.Size = new Size(width, ClientSize.Height - 2);
            VerticalScrollBar.Show();
        }
    }
}

3. 使用例

作成した ExtendedDataGridView は、ツールボックスからデザイナーにドラッグアンドドロップで追加できます。

ソースコードやデザイナーのプロパティから、スクロールバーの常時表示プロパティを変更できます。

4. 参考

おわりに

過去に 1 回だけ、案件で使ったことがあります。

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?