views:

2863

answers:

4

I have a Dictionary that contains items and prices. The items are unique but slowly get added and updated through the lifetime of the app (i.e. I don't know the item strings in advance). I would like to bind this structure to a DataGridView so I can show updates on my Form, something like:

Dictionary<string, double> _priceData = new Dictionary<string, double>();
BindingSource _bindingSource = new BindingSource();
dataGridView1.DataSource = _bindingSource;
_bindingSource.DataSource = _priceData;

But cannot, since Dictionary does not implement IList (or IListSource, IBindingList, or IBindingListView).

Can anybody offer a way to achieve this? I need to keep a unique list of items, but also update the price for an existing item, so a Dictionary is the ideal data structure I think, but I cannot find a way to display the data on my Form.

Thanks in advance, Will.


Update:

Marc's suggestion below works very nicely, but I'm still not sure how to update the DataGridView during execution.

I have a class-level variable:

private DictionaryBindingList<string, decimal> bList;

Then instantiate that in Main() :

bList = new DictionaryBindingList<string,decimal>(prices); 
dgv.DataSource = bList;

Then during program execution if a new entry is added to the dictionary:

prices.Add("foobar", 234.56M); bList.ResetBindings();

I thought that would refresh the DataGridView, but it doesn't?

+4  A: 

There are a couple of issues with dictionary; the first is (as you've found) it doesn't implement the necessary IList/IListSource. The second is that there is no guaranteed order to the items (and indeed, no indexer), making random access by index (rather than by key) impossible.

However... it is probably doable with some some smoke and mirrors; something like below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

static class Program
{
    [STAThread]
    static void Main()
    {
        Dictionary<string, decimal> prices =
            new Dictionary<string, decimal>();
        prices.Add("foo", 123.45M);
        prices.Add("bar", 678.90M);

        Application.EnableVisualStyles();
        Form form = new Form();
        DataGridView dgv = new DataGridView();
        dgv.Dock = DockStyle.Fill;
        form.Controls.Add(dgv);
        var bl = prices.ToBindingList();
        dgv.DataSource = bl;
        Button btn = new Button();
        btn.Dock = DockStyle.Bottom;
        btn.Click += delegate
        {
            prices.Add(new Random().Next().ToString(), 0.1M);
            bl.Reset();
        };
        form.Controls.Add(btn);
        Application.Run(form);
    }

    public static DictionaryBindingList<TKey, TValue>
        ToBindingList<TKey, TValue>(this IDictionary<TKey, TValue> data)
    {
        return new DictionaryBindingList<TKey, TValue>(data);
    }
    public sealed class Pair<TKey, TValue>
    {
        private readonly TKey key;
        private readonly IDictionary<TKey, TValue> data;
        public Pair(TKey key, IDictionary<TKey, TValue> data)
        {
            this.key = key;
            this.data = data;
        }
        public TKey Key { get { return key; } }
        public TValue Value
        {
            get
            {
                TValue value;
                data.TryGetValue(key, out value);
                return value;
            }
            set { data[key] = value; }
        }
    }
    public class DictionaryBindingList<TKey, TValue>
        : BindingList<Pair<TKey, TValue>>
    {
        private readonly IDictionary<TKey, TValue> data;
        public DictionaryBindingList(IDictionary<TKey, TValue> data)
        {
            this.data = data;
            Reset();
        }
        public void Reset()
        {
            bool oldRaise = RaiseListChangedEvents;
            RaiseListChangedEvents = false;
            try
            {
                Clear();
                foreach (TKey key in data.Keys)
                {
                    Add(new Pair<TKey, TValue>(key, data));
                }
            }
            finally
            {
                RaiseListChangedEvents = oldRaise;
                ResetBindings();
            }
        }

    }
}

Note that the use of a custom extension method is entirely optional, and can be removed in C# 2.0 etc by just using new DictionaryBindingList<string,decimal>(prices) instead.

Marc Gravell
I am very grateful for the help Marc, thanks.
WillH
This answer really helped me today. Thanks!
Matthew Talbert
+1  A: 

Make a class like so:

class MyRow
{
  public string key;
  public double value;
  public string Key {get {return key;}}
  public string Value {get {return value;}}
}

Then make a list of them:

List<MyRow> rows = new List<MyRow>();

Then insert them into that list, and databind to the list.

As an aside, if you've got LINQ, i think there's a ToArray method that'll simplify all this...

Chris
Lists don't easily enforce uniqueness, and it isn't easy to update a value in a list structure like that which is why I'd prefer to use a Dictionary.Thanks though.
WillH
+2  A: 

Or, in LINQ, its nice and quick:

var _priceDataArray = from row in _priceData select new { Item = row.Key, Price = row.Value };

That should then be bind-able, to the columns 'Item' and 'Price'

Chris
+1  A: 

For Dictionary<TKey, TValue> you can use these keywords for binding: Key and Value.

Here is example for ComboBox Binding, but it's possible to bind dictionary to DataGridView (set DataPropertyName for column to Key or Value).

    ComboBox1.DataSource =
        new BindingSource(Pricelevel.GetPricelevels(), null); // GetPricelevels() returns Dictionary<string, string>

    ComboBox1.ValueMember = "Key";
    ComboBox1.DisplayMember = "Value";
Vladislav