【C#】DataGridViewで編集不可やスクロールバーを常に表示する

この記事は最新更新日から、4年以上経過しています。

C#でテーブルを表示させるときはDataGridViewを使うみたいなので表示の設定。

だんたん、GUI上で右クリックして出てくるプロパティから編集不可にしたり行選択できるようにしたり設定するのがいやになってきたので書いておこうと思いました。

dataGridView1という名前のDataGridViewがあるとして、プロパティが並んでるところ(form1.Designer.csの該当箇所など)に書きましたが、フォームのもとのファイル(form1.csなど)に書いても適用されます。

編集不可にしたりする設定

// 
// dataGridView1
// 
//行を追加させない
this.dataGridView1.AllowUserToAddRows = false;

//行を削除させない
this.dataGridView1.AllowUserToDeleteRows = false;

//カラムの幅を変更させない
this.dataGridView1.AllowUserToResizeColumns = false;

//行の幅を変更させない
this.dataGridView1.AllowUserToResizeRows = false;

//複数行選択しない
this.dataGridView1.MultiSelect = false;

//読み取り専用(編集させない)
this.dataGridView1.ReadOnly = true;

//左端の空行を表示しない
this.dataGridView1.RowHeadersVisible = false;

//クリック時に全行選択する
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;

ヘッダ、行の背景色、文字色の変更

選択した際に色をつけたくない場合とかにも利用できる。

// Visualスタイルを無効にする(無効にしないと下記の文字色変更などが効かないので必須)
this.dataGridView1.EnableHeadersVisualStyles = false;


// セルの文字色変更
this.dataGridView1.ForeColor = Color.CornflowerBlue;

// グリッドの線の色変更
this.dataGridView1.GridColor = Color.Aqua;

// テーブルのセルのない灰色の部分の色の変更
this.dataGridView1.BackgroundColor = Color.Aquamarine;


// 列ヘッダの背景色と文字色(ForeColorは文字色のことらしい)
this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkGreen;
this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Crimson;

// 選択した列ヘッダの背景色と文字色
this.dataGridView1.ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.BlueViolet;
this.dataGridView1.ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.YellowGreen;


// 行ヘッダの背景色と文字色
this.dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.DarkGreen;
this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor = Color.Crimson;

// 選択した行ヘッダの背景色と文字色
this.dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.BlueViolet;
this.dataGridView1.RowHeadersDefaultCellStyle.SelectionForeColor = Color.YellowGreen;

常に縦スクロールバーを表示する

どうやらDataGridViewを継承したクラスを作ってそっちを使えばいいらしい?

always show the scrollbar in a datagridview
how can you do this? ... let's say that the datagridview has a big size and it only displays two records...i still want ...

とりあえずクラスファイルを作成して下記のようなクラスを作ります。

using System;
using System.Windows.Forms;
using System.Drawing;

namespace Hogehoge
{
    class CustomDataGridView : DataGridView
    {
        private const int CAPTIONHEIGHT = 1;
        private const int BORDERWIDTH = 2;

        /// <summary>
        /// Constructor
        /// </summary>        
        public CustomDataGridView() : base()
        {
            VerticalScrollBar.Visible = true;
            VerticalScrollBar.VisibleChanged += new EventHandler(VerticalScrollBar_VisibleChanged);
        }



        void VerticalScrollBar_VisibleChanged(object sender, EventArgs e)
        {
            if (!VerticalScrollBar.Visible)
            {
                int width = VerticalScrollBar.Width;

                VerticalScrollBar.Location =
                new Point(ClientRectangle.Width - width - BORDERWIDTH, CAPTIONHEIGHT);

                VerticalScrollBar.Size =
                new Size(width, ClientRectangle.Height - CAPTIONHEIGHT - BORDERWIDTH);

                VerticalScrollBar.Show();
            }
        }
    }
}

form1.Designer.csにDataGridViewを作った場合

上記のクラスをGUIで作った方のDataGridViewと置き換えます。
「form1.Designer.cs」的なファイルに下記のような感じの箇所があるので、「DataGridView()」を先程作った「CustomDataGridView()」置き換えます。
※このコードはかなり省略してあるので似たような雰囲気で修正してください。

private void InitializeComponent()
{
    //this.dataGridView1 = new System.Windows.Forms.DataGridView();
    this.dataGridView1 = new Hogehoge.CustomDataGridView();
}

//private System.Windows.Forms.DataGridView dataGridView1;   
private Hogehoge.CustomDataGridView dataGridView1;   

これで一応縦スクロールバーが出ました。
自動カラムリサイズにfillを指定していると、セルのデータにスクロールバーの表示が被ってしまうので、セルのカラムサイズに横幅指定したほうがきれいに表示されます。

おわり。