开发者

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 Strin开发者_运维百科gCollection _DirtyDRIClients;
public StringCollection DirtyDRIClients 
    { 
        get
        {
            return _DirtyDRIClients;
        }
        set
        {
            _DirtyDRIClients = Settings.Default.DRIUpdates;
        }
    }


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; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜