views:

73

answers:

2

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;
        }
    }
A: 

This post shows how to do it...

http://www.ranjanbanerji.com/techtalk/20060815.aspx

Leniel Macaferi
It does? I guess I am missing something....
Refracted Paladin
As the guy shows you have to expose a Value property. Data binding with primitives is one of the biggest problems. You can bind to an object and then display data from a property of that object. So if you had a DataGridView or a ListView in which you wish to display string contained in a String[], StringCollection, or List<string>, well you cannot. Because strings do not have a property that return the string itself. Actually you can bind a string to a control. It will display the length of the string as Length is the only property on the String object.
Leniel Macaferi
You have to workaround this limitation. The post shows you how to do that.
Leniel Macaferi
+1  A: 

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; }
}
Deleted Account