Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding String Collection to ListView, Windows Forms

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;
        }
    }
like image 698
Refracted Paladin Avatar asked Feb 03 '26 05:02

Refracted Paladin


1 Answers

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; }
}
like image 189
Deleted Account Avatar answered Feb 05 '26 19:02

Deleted Account



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!