0
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】ListViewの使い方

Last updated at Posted at 2025-09-06

環境

・Visual Studio 2022
・.Net 9.0
・Windows Forms

ListViewとは

ListViewは表やリストの表示を行いたいときに使えるコンポーネントです。
先頭の列に列名をいれたり、アイテム(セル)の周りに線を表示して区切りがわかりやすくしたりできます。
ただし拡張性はないのでカスタマイズしたい場合には向きません。

デザイン上での使い方

コンポーネントからListViewを選択して配置

スクリーンショット 2025-08-30 180923.png

列名を別で表示したい場合はColumnsが使えます。

スクリーンショット 2025-08-30 181041.png

追加ボタンでHeaderを追加し、Textに列名を入力。
Widthで横幅の長さを変更できます。

スクリーンショット 2025-08-31 173125.png

ViewをDetailsに設定すると設定したColumnが表示されます。

スクリーンショット 2025-08-30 181430.png

中身を追加したい場合は、Itemsを設定します。

スクリーンショット 2025-08-31 171832.png

追加ボタンで追加したListViewItemのSubItemsを設定する。
1つのListViewItemが1行分の情報を持っているイメージ。
Textに設定した文字が1列目の表示になります。

スクリーンショット 2025-08-31 171918.png

2列目以降はSubItemsで設定します。

スクリーンショット 2025-08-31 173525.png

最終的にはこんな感じになります。

スクリーンショット 2025-08-31 173720.png

コード上から設定

実際は中身の部分は取得したデータで設定すると思いますので、
コード上での設定方法も記載します。

void SetListView()
{
    this.listView1.Columns.Clear();
    // 列の追加
    // 列名、幅、テキストの配置
    this.listView1.Columns.Add("", 100, HorizontalAlignment.Left); // ダミー列
    this.listView1.Columns.Add("1", 100, HorizontalAlignment.Left);
    this.listView1.Columns.Add("2", 100, HorizontalAlignment.Left);
    this.listView1.Columns.Add("3", 100, HorizontalAlignment.Left);
    this.listView1.Columns.Add("4", 100, HorizontalAlignment.Left);

    this.listView1.Items.Clear();
    // 行の追加
    for (int i = 1; i <= 100; i++)
    {
        var num = i.ToString();
        // 1列目のテキストを引数に指定
        ListViewItem item = new ListViewItem(num);
        // 2列目以降のテキストを追加
        for (int j = 0; j < this.listView1.Columns.Count; j++)
        {
            item.SubItems.Add("item" + (j + 1).ToString());
        }
        this.listView1.Items.Add(item);
    }
}

スクリーンショット 2025-08-31 174749.png

株式会社ONE WEDGE

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