Getting Started with Windows Forms ListView

18 Nov 201821 minutes to read

This section explains how to get started with the WinForms SfListView control and perform grouping, sorting, filtering, and selection operations.

Assembly deployment

Refer to the Control Dependencies section for the complete list of assemblies and NuGet packages required for the ListView control.

Refer to NuGet Packages to learn how to install NuGet packages in a Windows Forms application.

To install via the NuGet Package Manager Console, run:

Install-Package Syncfusion.SfListView.WinForms

Adding SfListView via designer

  1. Create a new Windows Forms project in Visual Studio.

  2. You can add the SfListView to the application by dragging it from the Toolbox and dropping it onto the Windows Form designer. The required assembly references will be added automatically. The following dependent assemblies are added when the control is dropped on the form:

    • Syncfusion.SfListView.WinForms
    • Syncfusion.Core.WinForms
    • Syncfusion.DataSource.WinForms
    • Syncfusion.GridCommon.WinForms

Drag and drop the SfListView control into a WinForms application designer

Adding SfListView via code

To add the control manually, follow these steps:

  1. Add the following required assembly references to the project:

    • Syncfusion.Core.WinForms
    • Syncfusion.DataSource.WinForms
    • Syncfusion.GridCommon.WinForms
    • Syncfusion.SfListView.WinForms
  2. Create the SfListView control instance and add it to the form.

    using System.Drawing;
    using System.Windows.Forms;
    using Syncfusion.WinForms.ListView;
       
    namespace SfListViewDemo
    {
        public partial class Form1 : Form
        {
            private SfListView listView;
       
            public Form1()
            {
                InitializeComponent();
       
                listView = new SfListView();
                listView.Location = new Point(100, 100);
                listView.Size = new Size(300, 320);
       
                this.Controls.Add(listView);
            }
        }
    }
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports Syncfusion.WinForms.ListView
       
    Partial Public Class Form1
        Inherits Form
       
        Private listView As SfListView
       
        Public Sub New()
            InitializeComponent()
       
            listView = New SfListView()
            listView.Location = New Point(100, 100)
            listView.Size = New Size(300, 320)
            Me.Controls.Add(listView)
        End Sub
    End Class

Create the data model

Create a simple data model that represents each item in the list.

  1. Add a class named CountryInfo with the CountryName and Continent properties.

    public class CountryInfo
    {
        public string CountryName { get; set; }
        public string Continent { get; set; }
    }
    Public Class CountryInfo
        Public Property CountryName() As String
        Public Property Continent() As String
    End Class
  2. Add the GetDataSource method to return a populated collection of CountryInfo objects.

    using System.Collections.Generic;
       
    // Place this method on Form1 (next to the constructor) so the call in the constructor compiles.
    public List<CountryInfo> GetDataSource()
    {
        List<CountryInfo> countryInfoCollection = new List<CountryInfo>();
        countryInfoCollection.Add(new CountryInfo() { CountryName = "China", Continent = "Asia" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "India", Continent = "Asia" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Japan", Continent = "Asia" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Malaysia", Continent = "Asia" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Singapore", Continent = "Asia" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Kenya", Continent = "Africa" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Nigeria", Continent = "Africa" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "South Africa", Continent = "Africa" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Uganda", Continent = "Africa" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Zimbabwe", Continent = "Africa" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "France", Continent = "Europe" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Germany", Continent = "Europe" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Italy", Continent = "Europe" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Spain", Continent = "Europe" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "United Kingdom", Continent = "Europe" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Canada", Continent = "North America" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Cuba", Continent = "North America" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Jamaica", Continent = "North America" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Mexico", Continent = "North America" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "United States of America", Continent = "North America" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Australia", Continent = "Oceania" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "New Zealand", Continent = "Oceania" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Argentina", Continent = "South America" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Brazil", Continent = "South America" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Chile", Continent = "South America" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Colombia", Continent = "South America" });
        countryInfoCollection.Add(new CountryInfo() { CountryName = "Uruguay", Continent = "South America" });
        return countryInfoCollection;
    }
    ' Place this method on Form1 (next to the constructor) so the call in the constructor compiles.
    Private Function GetDataSource() As List(Of CountryInfo)
        Dim countryInfoCollection As New List(Of CountryInfo)()
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "China", .Continent = "Asia"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "India", .Continent = "Asia"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Japan", .Continent = "Asia"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Malaysia", .Continent = "Asia"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Singapore", .Continent = "Asia"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Kenya", .Continent = "Africa"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Nigeria", .Continent = "Africa"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "South Africa", .Continent = "Africa"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Uganda", .Continent = "Africa"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Zimbabwe", .Continent = "Africa"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "France", .Continent = "Europe"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Germany", .Continent = "Europe"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Italy", .Continent = "Europe"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Spain", .Continent = "Europe"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "United Kingdom", .Continent = "Europe"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Canada", .Continent = "North America"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Cuba", .Continent = "North America"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Jamaica", .Continent = "North America"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Mexico", .Continent = "North America"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "United States of America", .Continent = "North America"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Australia", .Continent = "Oceania"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "New Zealand", .Continent = "Oceania"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Argentina", .Continent = "South America"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Brazil", .Continent = "South America"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Chile", .Continent = "South America"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Colombia", .Continent = "South America"})
        countryInfoCollection.Add(New CountryInfo() With {.CountryName = "Uruguay", .Continent = "South America"})
        Return countryInfoCollection
    End Function

Bind to data

Set the DataSource property to an IEnumerable collection and use the DisplayMember property to specify the data field to display.

// Inside the Form1 constructor, after creating listView:
   listView.DataSource = GetDataSource();
   listView.DisplayMember = "CountryName";
' Inside the Form1 constructor, after creating listView:
   listView.DataSource = GetDataSource()
   listView.DisplayMember = "CountryName"

Data binding in WinForms SfListView Control

Grouping

ListView supports grouping items based on a data property through its View (Syncfusion.DataSource.DataSource) object by using the View.GroupDescriptors collection. Create a GroupDescriptor for the Continent property and add it to the collection.

GroupDescriptor exposes the following properties:

  • PropertyName – Specifies the property name used for grouping.
  • KeySelector – Specifies a selector that returns the group key.
  • Comparer – Specifies the comparer used to sort groups.

    using Syncfusion.DataSource;
       
     listView.View.GroupDescriptors.Add(new GroupDescriptor()
     {
         PropertyName = "Continent",
     });
    Imports Syncfusion.DataSource
       
     listView.View.GroupDescriptors.Add(New GroupDescriptor() With {.PropertyName = "Continent"})

Grouping in WinForms SfListView Control

Sorting

ListView supports sorting data using the View.SortDescriptors collection. Create a SortDescriptor and add it to the collection.

SortDescriptor exposes the following properties:

  • PropertyName – Specifies the property used for sorting.
  • Direction – Specifies the sort direction as a System.ComponentModel.ListSortDirection value (Ascending or Descending).
  • Comparer – Specifies the comparer used for sorting.

    using System.ComponentModel; // ListSortDirection
     using Syncfusion.DataSource; // SortDescriptor
       
     listView.View.SortDescriptors.Add(new SortDescriptor()
     {
         PropertyName = "Continent",
         Direction = ListSortDirection.Descending,
     });
    Imports System.ComponentModel ' ListSortDirection
     Imports Syncfusion.DataSource ' SortDescriptor
       
     listView.View.SortDescriptors.Add(New SortDescriptor() With {
         .PropertyName = "Continent",
         .Direction = ListSortDirection.Descending
     })

Sorting in WinForms SfListView Control

Filtering

ListView supports filtering items by assigning a predicate method to the View.Filter property. Call View.RefreshFilter after each Filter assignment to apply the filtering changes.

The following example displays only the countries that belong to Asia, North America, or Oceania.

// Inside Form1:
   listView.View.Filter = CustomFilter;
   listView.View.RefreshFilter();
   
   // Private method on Form1:
   private bool CustomFilter(object obj)
   {
       var item = obj as CountryInfo;
       return item != null &&
              (item.Continent == "Asia" ||
               item.Continent == "North America" ||
               item.Continent == "Oceania");
   }
' Inside Form1:
   listView.View.Filter = AddressOf CustomFilter
   listView.View.RefreshFilter()
   
   ' Private method on Form1:
   Private Function CustomFilter(obj As Object) As Boolean
       Dim item = TryCast(obj, CountryInfo)
       Return item IsNot Nothing AndAlso
              (item.Continent = "Asia" OrElse
               item.Continent = "North America" OrElse
               item.Continent = "Oceania")
   End Function

Filtering in WinForms SfListView Control

Selection

Configure item selection behavior by setting the SelectionMode property to one of the following values.

SelectionMode Description
One Only a single item can be selected at a time.
MultiSimple Multiple items can be selected without holding a modifier key.
MultiExtended Multiple items can be selected using Ctrl/Shift.
None Selection is disabled.

The currently selected items can be accessed through the SelectedItem, SelectedIndex, and SelectedItems. Selection changes can be tracked using the SelectionChanging and SelectionChanged events.

using Syncfusion.WinForms.ListView;
   using Syncfusion.WinForms.ListView.Events;
   
   listView.SelectionMode = SelectionMode.One;
   listView.SelectionChanged += ListView_SelectionChanged;
   
   private void ListView_SelectionChanged(object sender, ItemSelectionChangedEventArgs e)
   {
       var selected = listView.SelectedItem as CountryInfo;
   }
Imports Syncfusion.WinForms.ListView
   Imports Syncfusion.WinForms.ListView.Events
   
   listView.SelectionMode = SelectionMode.One
   AddHandler listView.SelectionChanged, AddressOf ListView_SelectionChanged
   
   Private Sub ListView_SelectionChanged(sender As Object, e As ItemSelectionChangedEventArgs)
       Dim selected = TryCast(listView.SelectedItem, CountryInfo)
   End Sub

Item selection in WinForms SfListView Control