c# windowsフォームの表形式でデータを一覧表示する（DataGridView）

windowsフォームでデータを一覧表示する方法
ツールボックスからDataGridViewのコントロールを追加する

![dataGridView.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/482880/2b5e1c68-2f95-73f0-50bd-8835d47bcbf0.jpeg)


## DataGridView

・データの一覧を表形式で表示できる
・行、列の追加が可能
・ボタンなども付けられる

```C#:FormMain.cs
using SampleDataGridView.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SampleDataGridView
{
    public partial class FormMain : Form
    {
        private List<SampleDataSource> _dataList;

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            try
            {
                _dataList = new List<SampleDataSource>()
                {
                    new SampleDataSource(){ Id = 1,  UserName = "Tom", Language = "C#", Age = 25},
                    new SampleDataSource(){ Id = 2,  UserName = "Jhon", Language = "PHP", Age = 32},
                    new SampleDataSource(){ Id = 3,  UserName = "Arthur", Language = "Python", Age = 21},
                    new SampleDataSource(){ Id = 4,  UserName = "Carl", Language = "Java", Age = 28},
                    new SampleDataSource(){ Id = 5,  UserName = "Eden", Language = "Rust", Age = 35},
                    new SampleDataSource(){ Id = 6,  UserName = "Billy", Language = "Go", Age = 36},
                    new SampleDataSource(){ Id = 7,  UserName = "Neal", Language = "Ruby", Age = 42},
                    new SampleDataSource(){ Id = 8,  UserName = "Jakob", Language = "C++", Age = 38},
                    new SampleDataSource(){ Id = 9,  UserName = "Frank", Language = "Swift", Age = 22},
                    new SampleDataSource(){ Id = 10,  UserName = "Madison", Language = "VB", Age = 29},
                };

                LoadDataIntoDataGridView(_dataList);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,
                    "Error.",
                    MessageBoxButtons.AbortRetryIgnore,
                    MessageBoxIcon.Error);
            }
        }

        private void LoadDataIntoDataGridView(List<SampleDataSource> dataList)
        {
            // データを読み込む
            dataGridViewMain.DataSource = dataList;            
        }
    }
}
```

![dataGridView_loadData.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/482880/07048315-e6c7-cafb-2cd4-a80319fd5068.jpeg)


カラムを追加したり、操作ボタンをつけることも可能

```C#:FromMain.cs
private void LoadDataIntoDataGridView(List<SampleDataSource> dataList)
{
    // 列とボタン追加
    dataGridViewMain.Columns.Add(new DataGridViewButtonColumn() { Name = "action", Text = "select",　Width = 50, UseColumnTextForButtonValue = true });

    // データを読み込む
    dataGridViewMain.DataSource = dataList;                       
}

private void dataGridViewMain_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    // クリックした行のインデックスを取得
    string selectedRowIndex = e.RowIndex.ToString();
    MessageBox.Show(selectedRowIndex);
}
```
![selected_row.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/482880/d51066f9-32e3-5477-3c8e-ba31cb4fa661.jpeg)

今回作成したサンプルはこちら
https://github.com/pixcelo/windows_form/tree/main/dataGridView/SampleDataGridView

## Reference
[DataGridView クラス (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.datagridview?view=windowsdesktop-6.0)