1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WinForms DataGridView

Last updated at Posted at 2023-09-03
  • ネットで見つけた業務アプリで使えそうな設定やサンプル。
  • ボタン付け,バー表示,左の三角マーク消し,下の三角マーク消し 等
  • 標準でソートも出来る。
  • データは内蔵してるので実務ではDB等から読みだしたデータを表示。
  • 該当行のダブルクリックなり追加したボタンの投下で詳細出したり。簡単にアプリに応用可能。
    1.JPG
using System;
using System.Windows.Forms;
using System.Drawing;

namespace exce{
    public partial class Form1 : Form    {
        public Form1()        {
            InitializeComponent();

            dataGridView1.ColumnCount = 4;
            
            // カラム名を指定
            dataGridView1.Columns[0].HeaderText = "教科";
            dataGridView1.Columns[1].HeaderText = "点数";
            dataGridView1.Columns[2].HeaderText = "氏名";
            dataGridView1.Columns[3].HeaderText = "クラス名";

            //dataGridView1.Columns[3].ReadOnly = true; // 書込み不可

            // データを追加
            dataGridView1.Rows.Add("国語", "90", "田中 一郎", "A");
            dataGridView1.Rows.Add("数学", "50", "鈴木 二郎", "A");
            dataGridView1.Rows.Add("英語", "90", "佐藤 三郎", "B");
            dataGridView1.Rows.Add("国語", "90", "阿佐ヶ一郎", "A");
            dataGridView1.Rows.Add("数学", "50", "鈴木 二郎", "A");
            dataGridView1.Rows.Add("国語", "90", "田中 一郎", "A");
            dataGridView1.Rows.Add("数学", "50", "織田  信成", "A");
            dataGridView1.Rows.Add("英語", "90", "佐藤 三郎", "B");
            dataGridView1.Rows.Add("国語", "90", "田中 一郎", "A");
            dataGridView1.Rows.Add("数学", "50", "鈴木 二郎", "A");
            dataGridView1.Rows.Add("国語", "90", "田中 一郎", "A");
            dataGridView1.Rows.Add("数学", "50", "鈴木 二郎", "A");
            dataGridView1.Rows.Add("英語", "90", "佐藤 三郎", "B");
            dataGridView1.Rows.Add("国語", "90", "田中 一郎", "A");
            dataGridView1.Rows.Add("数学", "50", "鈴木 二郎", "A");

            

            ////////////////////////////////////////////////////////////////////////
            // ボタン付け
            DataGridViewButtonColumn column = new DataGridViewButtonColumn();
            //列の名前を設定
            column.Name = "選択";
            column.UseColumnTextForButtonValue = true;
            column.Text = "--";

            //DataGridViewに追加する
            dataGridView1.Columns.Add(column);
                     
            DataGridViewButtonColumn column2 = new DataGridViewButtonColumn();
            //列の名前を設定
            column2.Name = "削除";
            column2.UseColumnTextForButtonValue = true;
            column2.Text = "--";

            //DataGridViewに追加する
            dataGridView1.Columns.Add(column2);
            ////////////////////////////////////////////////////////////////////////

                                 
            // 幅
            dataGridView1.Columns[1].Width = 50;

            // ヘッダセンタリング
            dataGridView1.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

            // DEL禁止
            dataGridView1.AllowUserToDeleteRows = false;

            // 左の三角マーク消し
            dataGridView1.RowHeadersVisible = false;

            // 下の三角マーク消し
            dataGridView1.AllowUserToAddRows = false;


            // FONT
            //dataGridView1.DefaultCellStyle.Font = new Font("HGS創英角ポップ体", 10, FontStyle.Bold);

            // 指定セル取得
            // MessageBox.Show(dataGridView1.Rows[0].Cells[0].Value.ToString());

            // ソート禁止
            //dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;


            // 列の幅、行の高さを変更させないようにする
            // AllowUserToResizeColumns → False
            // AllowUserToResizeRows     → False

            // 複数行選択できないようにする MultiSelect → False
            // 編集させないようにする ReadOnly → True
            // 奇数行の色を変える AlternatingRowsDefaultCellStyleをクリック BackColor → 色設定
            // 任意のセルを選択すると1行選択にする SelectionMode → FullRowSelect


            // アイコン
            DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
            imageColumn.Image = Bitmap.FromFile(@"C:\sync.ico");   //ファイルからイメージを読み込み
            imageColumn.Name = "Save";
            imageColumn.HeaderText = "Save";
            dataGridView1.Columns.Insert(2, imageColumn);

        }

        private void Form1_Load(object sender, EventArgs e)        {
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
       
            if (dgv.Columns[e.ColumnIndex].Name == "選択"){
                                  
                MessageBox.Show(dataGridView1.CurrentRow.Cells[1].Value.ToString());
            }

            if (dgv.Columns[e.ColumnIndex].Name == "削除") {

                MessageBox.Show(dataGridView1.CurrentRow.Cells[2].Value.ToString());
            }


        }


        // バー表示
        // https://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=31744&forum=7
        private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)        {

            if (e.RowIndex >= 0)            {
                DataGridViewCellStyle tcs = new DataGridViewCellStyle();
                tcs.SelectionBackColor = Color.Blue;
                dataGridView1.AlternatingRowsDefaultCellStyle = tcs;

                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                foreach (DataGridViewRow r in dataGridView1.Rows){
                    r.Selected = false;
                }

                dataGridView1.Rows[e.RowIndex].Selected = true;

            }

        }
    }
}
1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?