1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WindowsForm C# DataGridViewでFullRowSelectの使用方法

Posted at

この記事は、WindowsフォームのDataGridViewでセルをクリックした際に行全体を選択する方法について説明です。DataGridViewの選択モードをFullRowSelectに設定すると、列ヘッダーに不要な選択背景色が表示されてしまうので以下の通り対応しました。

DataGridViewのデフォルト表示

image.png

以下のCellPaintingイベントでコードを追加して、DataGridViewで行を選択したときに列ヘッダーに選択色が表示されないようにしました。

TestForm.cs
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WinFormsDemo.Forms
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            // DataGridView に列を追加
            dataGridView2.Columns.Add("Id", "ID");       // ID 用の列を追加
            dataGridView2.Columns.Add("Name", "名前");    // 名前用の列を追加
            // DataGridView の列を DataGridView の全幅に合わせてサイズ変更
            dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            // DataGridViewの選択モードをFullRowSelectに設定する
            dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            // DataGridView に行を追加
            dataGridView2.Rows.Add("1", "たなか");
            dataGridView2.Rows.Add("2", "しまだ");
            dataGridView2.Rows.Add("3", "太郎");
            dataGridView2.Rows.Add("4", "れいな");
        }

        private void dataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            // 列ヘッダー行の選択色を防止
            if (e.RowIndex == -1 && e.ColumnIndex >= 0)
            {
                e.Handled = true;
                // 列ヘッダーの背景色を塗る
                using (Brush headerBrush = new SolidBrush(dataGridView2.ColumnHeadersDefaultCellStyle.BackColor))
                {
                    e.Graphics.FillRectangle(headerBrush, e.CellBounds);
                }
                // 列ヘッダーのテキストを描画
                TextRenderer.DrawText(
                    e.Graphics,
                    e.Value?.ToString(),
                    e.CellStyle.Font,
                    e.CellBounds,
                    dataGridView2.ColumnHeadersDefaultCellStyle.ForeColor,
                    TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter
                );
                // ヘッダーセルの周りにデフォルトの枠線スタイルを描画
                ControlPaint.DrawBorder(
                    e.Graphics,
                    e.CellBounds,
                    dataGridView2.GridColor, // 枠線の色
                    ButtonBorderStyle.Solid // 枠線スタイル
                );
            }
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                textBoxId.Text = dataGridView2.Rows[e.RowIndex].Cells["Id"].Value.ToString();
                textBoxName.Text = dataGridView2.Rows[e.RowIndex].Cells["Name"].Value.ToString();
            }
        }
    }
}

修正した後DataGridViewの表示

image.png
:kissing_closed_eyes: ☆☆☆

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?