ListView
A ListView is the control for a list of records with a few columns: files in a folder, orders in a day, devices on a network. It is lighter than a DataGridView and it does not bind, which makes it the right choice for a list that is filled by hand and read by the user, and the wrong one for a table that is edited.
A list with columns
using System;
using System.Drawing;
using System.Windows.Forms;
internal static class Program
{
[STAThread]
private static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
internal sealed class Form1 : Form
{
private readonly ListView _files = new()
{
Dock = DockStyle.Fill,
View = View.Details,
FullRowSelect = true,
GridLines = true,
MultiSelect = true,
HideSelection = false,
};
public Form1()
{
Text = "Files";
ClientSize = new Size(520, 320);
_files.Columns.Add("Name", 220, HorizontalAlignment.Left);
_files.Columns.Add("Size", 90, HorizontalAlignment.Right);
_files.Columns.Add("Modified", 140, HorizontalAlignment.Left);
var notes = new ListViewItem("notes.txt") { Tag = "C:/notes/notes.txt" };
notes.SubItems.Add("1 200");
notes.SubItems.Add("2026-11-20 09:41");
var ideas = new ListViewItem("ideas.txt") { Tag = "C:/notes/ideas.txt" };
ideas.SubItems.Add("340");
ideas.SubItems.Add("2026-11-21 18:03");
_files.Items.Add(notes);
_files.Items.Add(ideas);
_files.SelectedIndexChanged += (sender, e) =>
Text = $"{_files.SelectedItems.Count} selected, first: {_files.FocusedItem?.Text}";
Controls.Add(_files);
}
}
dotnet build app.csproj
Build succeeded.
0 Warning(s)
0 Error(s)
The listing is built and not run. A row is a ListViewItem whose Text is the first column, and every further column is a SubItem added in order: the count of sub-items has to match the count of columns, and the control does not complain when it does not. Tag carries the full path, which is the id the program needs when a row is opened.
Items and sub-items
- Items and SelectedItems: all rows and the selected ones. FocusedItem is the row with the focus rectangle, which is not the same as the first selected row.
- Items[i].SubItems[j].Text: a cell. The first sub-item is the column after the name, and SubItems[0] is the name itself, which surprises a reader once.
- CheckedItems: the rows whose check box is ticked, with CheckBoxes = true. There is no CheckedItems selection event of its own; the tick raises ItemChecked.
- Groups: a ListViewGroup per heading, for a list that is read in sections. The group of a row is a property.
- VirtualMode: the control asks for a row when it needs it with RetrieveVirtualItem, which is how a list of a hundred thousand records scrolls without a hundred thousand objects.
The five views
- View.Details: the columns of this lesson. The only view that shows more than one field per row.
- View.LargeIcon and View.SmallIcon: a tile per row, with an image from LargeImageList or SmallImageList. The view of a folder of pictures.
- View.List: one column of small icons, which is a list of names.
- View.Tile: a large icon with two lines of text, which is the view of a music library.
The image lists are real controls: ImageList holds images of one size, the ListView refers to them by index, and ImageKey names one instead. An ImageList that is not disposed is a leak of every image in it, which is why a form disposes it with itself.
Sorting by a column
A ListView does not sort itself in View.Details. The pattern is a click on a column header and a sort of the items, and the comparison is the part with the subtlety: the size column holds text, so "1 200" sorts before "340" unless the comparison parses it.
using System;
using System.Collections;
using System.Windows.Forms;
internal sealed class FileComparer : IComparer
{
private readonly int _column;
public FileComparer(int column) => _column = column;
public int Compare(object? x, object? y)
{
var left = x as ListViewItem;
var right = y as ListViewItem;
if (left is null || right is null)
{
return 0;
}
var a = left.SubItems[_column].Text;
var b = right.SubItems[_column].Text;
if (int.TryParse(a, out var numberA) && int.TryParse(b, out var numberB))
{
return numberA.CompareTo(numberB);
}
return string.Compare(a, b, StringComparison.CurrentCulture);
}
}
That block is a fragment: a class on its own, so the checker counts it and does not build it. ListViewItemComparer is the class the documentation names for this job, and its shape is the one above: a column number, a Compare that reads the sub-item, and a numeric parse before a string comparison. The form wires it in the column click handler and sets _files.ListViewItemSorter to it.
A sorted list that runs
The rows of the table and their order are data, and the sort runs here:
using System;
using System.Collections.Generic;
using System.Linq;
var rows = new List<Row>
{
new("notes.txt", 1200, "2026-11-20"),
new("ideas.txt", 340, "2026-11-21"),
new("todo.txt", 9800, "2026-11-19"),
};
foreach (var row in rows.OrderBy(row => row.Size))
{
Console.WriteLine($"{row.Name,-10} {row.Size,6} {row.Date}");
}
Console.WriteLine($"largest: {rows.Max(row => row.Size)} bytes, rows: {rows.Count}");
Console.WriteLine($"sorted by name: {string.Join(", ", rows.OrderBy(row => row.Name).Select(row => row.Name))}");
internal sealed record Row(string Name, int Size, string Date);
dotnet run
ideas.txt 340 2026-11-21
notes.txt 1200 2026-11-20
todo.txt 9800 2026-11-19
largest: 9800 bytes, rows: 3
sorted by name: ideas.txt, notes.txt, todo.txt
The output is the real run. The size column is a number and sorts as one, which is the rule the comparer above enforces and the reason a string sort of the same column would be wrong. The last line is the same list ordered by another key, which is what a click on a different header does: the data does not change, the order does, and the form keeps the key it last used so that a second click reverses it.
Exercises
- Add a fourth row and run the program. What does largest print?
- Sort by date instead of size and run it again.
- Explain from the documentation why SubItems[0] is the name of the row and not its first extra column.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which View shows columns?
Answer
View.Details
The other views show an image and one line of text per row.
-
How is a further column of a row filled?
Answer
With SubItems.Add, in column order
Columns.Add defines the column; SubItems.Add fills it for one row.
-
What is SubItems[0]?
Answer
The name of the row, the first column
The item's Text is the first sub-item.
-
Which property makes a whole row selected rather than its first cell?
Answer
FullRowSelect = true
Without it a click selects the item, not the row across the columns.
-
What does VirtualMode do?
Answer
Asks for a row when it is needed instead of holding them all
It is how a list of a hundred thousand rows scrolls.
-
Which event reports a tick of a row's check box?
Answer
ItemChecked
CheckBoxes = true adds the box; CheckedItems reads the result.
-
Why does ListView not sort itself in Details view?
Answer
The comparison depends on the data, so the program supplies it
ListViewItemSorter takes an IComparer written for the columns.
-
What did the run print first?
Answer
ideas.txt, the smallest file
The list is ordered by size, ascending.
-
Why would a string sort of the size column be wrong?
Answer
"1200" sorts before "340" as text
The comparer parses the number before it compares.
-
Which class holds the images for the icon views?
Answer
ImageList
LargeImageList and SmallImageList point at two of them.
Every listing on this page was built with the .NET SDK 10.0.303 for net10.0-windows against the targeting pack Microsoft.WindowsDesktop.App.Ref 10.0.15 from the offline feed, in a Docker container on Alpine Linux 3.24.3 (x86_64). The programs were built, not run on Linux: WinForms needs Windows. The logic that draws nothing was also run headless on the same machine, and its output is the run output printed below each of those programs.
Article author: Arthur Isaev