2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Posted at

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

dataGridView.jpg

DataGridView

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

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

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

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://github.com/pixcelo/windows_form/tree/main/dataGridView/SampleDataGridView

Reference

DataGridView クラス (System.Windows.Forms) | Microsoft Docs

2
5
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
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?