Binding String Collection to ListView, Windows Forms

16,998

You cannot actually bind to a ListView control, as it does not support binding. You need to add the items programmatically. You can however bind to a ListBox, although as others have said you cannot bind strings directly, you need to create a wrapper for them. Something like this...

public partial class Form1 : Form
{
    List<Item> items = new List<Item>
    {
        new Item { Value = "One" },
        new Item { Value = "Two" },
        new Item { Value = "Three" },
    };

    public Form1()
    {
        InitializeComponent();

        listBox1.DataSource = items;
        listBox1.DisplayMember = "Value";
    }
}

public class Item
{
    public string Value { get; set; }
}
Share:
16,998
Refracted Paladin
Author by

Refracted Paladin

My Philosophy

Updated on June 04, 2022

Comments

  • Refracted Paladin
    Refracted Paladin almost 2 years

    I have a StringCollection that I want to One Way Bind to a ListView. As in, the ListView should display the contents of the StringCollection. I will be removing items from the collection programatically so they don't need to interact with it through the ListView.

    I have a Form with a Property, like so -->

    public DRIUploadForm()
        {
            InitializeComponent();
    
            lvwDRIClients.DataBindings.Add("Items", this.DirtyDRIClients, "DirtyDRIClients");
        }
    
    private StringCollection _DirtyDRIClients;
    public StringCollection DirtyDRIClients 
        { 
            get
            {
                return _DirtyDRIClients;
            }
            set
            {
                _DirtyDRIClients = Settings.Default.DRIUpdates;
            }
        }