Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Welcome to the wonderful world of System.ComponentModel. This dark corner of .NET is very powerful, but very complex.</p> <p>A word of caution; unless you have a lot of time for this - you may do well to simply serialize it in whatever mechanism you are happy with, but rehydrate it back into a <code>DataTable</code> at each end... what follows is not for the faint-hearted ;-p</p> <p>Firstly - data binding (for tables) works against <em>lists</em> (<code>IList</code>/<code>IListSource</code>) - so <code>List&lt;T&gt;</code> should be fine (edited: I misread something). But it isn't going to understand that your dictionary is actually columns...</p> <p>To get a type to pretend to have columns you need to use custom <code>PropertyDescriptor</code> implementations. There are several ways to do this, depending on whether the column definitions are always the same (but determined at runtime, i.e. perhaps from config), or whether it changes per usage (like how each <code>DataTable</code> instance can have different columns).</p> <p>For "per instance" customisation, you need to look at <code>ITypedList</code> - this beast (implemented in <em>addition</em> to <code>IList</code>) has the fun task of presenting properties for tabular data... but it isn't alone:</p> <p>For "per type" customisation, you can look at <code>TypeDescriptionProvider</code> - this can suggest dynamic properties for a class...</p> <p>...or you can implement <code>ICustomTypeDescriptor</code> - but this is only used (for lists) in <strong>very</strong> occasional circumstances (an object indexer (<code>public object this[int index] {get;}</code>") and at least one row in the list at the point of binding). (this interface is much more useful when binding discrete objects - i.e. not lists).</p> <p>Implementing <code>ITypedList</code>, and providing a <code>PropertyDescriptor</code> model is hard work... hence it is only done very occasionally. I'm fairly familiar with it, but I wouldn't do it just for laughs...</p> <hr> <p>Here's a <strong>very, very simplified</strong> implementation (all columns are strings; no notifications (via descriptor), no validation (<code>IDataErrorInfo</code>), no conversions (<code>TypeConverter</code>), no additional list support (<code>IBindingList</code>/<code>IBindingListView</code>), no abstraction (<code>IListSource</code>), no other other metadata/attributes, etc):</p> <pre><code>using System.ComponentModel; using System.Collections.Generic; using System; using System.Windows.Forms; static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); PropertyBagList list = new PropertyBagList(); list.Columns.Add("Foo"); list.Columns.Add("Bar"); list.Add("abc", "def"); list.Add("ghi", "jkl"); list.Add("mno", "pqr"); Application.Run(new Form { Controls = { new DataGridView { Dock = DockStyle.Fill, DataSource = list } } }); } } class PropertyBagList : List&lt;PropertyBag&gt;, ITypedList { public PropertyBag Add(params string[] args) { if (args == null) throw new ArgumentNullException("args"); if (args.Length != Columns.Count) throw new ArgumentException("args"); PropertyBag bag = new PropertyBag(); for (int i = 0; i &lt; args.Length; i++) { bag[Columns[i]] = args[i]; } Add(bag); return bag; } public PropertyBagList() { Columns = new List&lt;string&gt;(); } public List&lt;string&gt; Columns { get; private set; } PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors) { if(listAccessors == null || listAccessors.Length == 0) { PropertyDescriptor[] props = new PropertyDescriptor[Columns.Count]; for(int i = 0 ; i &lt; props.Length ; i++) { props[i] = new PropertyBagPropertyDescriptor(Columns[i]); } return new PropertyDescriptorCollection(props, true); } throw new NotImplementedException("Relations not implemented"); } string ITypedList.GetListName(PropertyDescriptor[] listAccessors) { return "Foo"; } } class PropertyBagPropertyDescriptor : PropertyDescriptor { public PropertyBagPropertyDescriptor(string name) : base(name, null) { } public override object GetValue(object component) { return ((PropertyBag)component)[Name]; } public override void SetValue(object component, object value) { ((PropertyBag)component)[Name] = (string)value; } public override void ResetValue(object component) { ((PropertyBag)component)[Name] = null; } public override bool CanResetValue(object component) { return true; } public override bool ShouldSerializeValue(object component) { return ((PropertyBag)component)[Name] != null; } public override Type PropertyType { get { return typeof(string); } } public override bool IsReadOnly { get { return false; } } public override Type ComponentType { get { return typeof(PropertyBag); } } } class PropertyBag { private readonly Dictionary&lt;string, string&gt; values = new Dictionary&lt;string, string&gt;(); public string this[string key] { get { string value; values.TryGetValue(key, out value); return value; } set { if (value == null) values.Remove(key); else values[key] = value; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload