Example E1075
Visible to All Users

WinForms Data Grid - Bind to a two-dimensional array

The WinForms Data Grid does not support multidimensional data sources out of the box. This example creates a wrapper class for 2D-arrays that implements IList and ITypedList interfaces.

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

WindowsApplication167/Array2DWrapper.cs(vb)
C#
using System; using System.Text; using System.Collections.Generic; using System.Collections; using System.ComponentModel; namespace Example { public class Array2DWrapper<T> : IList, ITypedList { class WrapperPropertyDescriptor : PropertyDescriptor { int index; Type elementType; public WrapperPropertyDescriptor(string name, int index, Type elementType) : base(name, null) { this.index = index; this.elementType = elementType; } public override bool CanResetValue(object component) { return false; } public override Type ComponentType { get { return typeof(RowWrapper); } } public override object GetValue(object component) { if (component is RowWrapper) return ((RowWrapper)component).GetValue(this.index); return null; } public override bool IsReadOnly { get { return false; } } public override Type PropertyType { get { return elementType; } } public override void ResetValue(object component) { } public override void SetValue(object component, object value) { if (component is RowWrapper) ((RowWrapper)component).SetValue(this.index, value); } public override bool ShouldSerializeValue(object component) { return false; } } class RowWrapper : CustomTypeDescriptor { Array2DWrapper<T> owner; public RowWrapper(Array2DWrapper<T> owner) { this.owner = owner; } public override PropertyDescriptorCollection GetProperties() { return base.GetProperties(); } public object GetValue(int index) { return owner.array.GetValue(owner.list.IndexOf(this), index); } public void SetValue(int index, object value) { owner.array.SetValue(value, owner.list.IndexOf(this), index); } public override PropertyDescriptorCollection GetProperties(Attribute[] attributes) { return owner.pdc; } } private T[,] array; private List<RowWrapper> list; public Array2DWrapper(T[,] array) { this.array = array; int count = array.GetLength(0); list = new List<RowWrapper>(count); for (int i = 0; i < count; i++) list.Add(new RowWrapper(this)); } #region IList Members int IList.Add(object value) { throw new Exception("The method or operation is not implemented."); } void IList.Clear() { throw new Exception("The method or operation is not implemented."); } bool IList.Contains(object value) { if (value is RowWrapper) return list.Contains((RowWrapper)value); return false; } int IList.IndexOf(object value) { if (value is RowWrapper) return list.IndexOf((RowWrapper)value); return -1; } void IList.Insert(int index, object value) { throw new Exception("The method or operation is not implemented."); } bool IList.IsFixedSize { get { return true; } } bool IList.IsReadOnly { get { return true; } } void IList.Remove(object value) { throw new Exception("The method or operation is not implemented."); } void IList.RemoveAt(int index) { throw new Exception("The method or operation is not implemented."); } object IList.this[int index] { get { return list[index]; } set { throw new Exception("The method or operation is not implemented."); } } #endregion #region ICollection Members void ICollection.CopyTo(Array array, int index) { if (array is RowWrapper[]) list.CopyTo((RowWrapper[])array, index); } int ICollection.Count { get { return list.Count; } } bool ICollection.IsSynchronized { get { return false; } } object ICollection.SyncRoot { get { return this; } } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { return list.GetEnumerator(); } #endregion #region ITypedList Members PropertyDescriptorCollection pdc; PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors) { if (pdc == null) { PropertyDescriptor[] pd = new PropertyDescriptor[array.GetLength(1)]; for (int i = 0; i < pd.Length; i++) { pd[i] = new WrapperPropertyDescriptor("C" + i, i, typeof(T)); } pdc = new PropertyDescriptorCollection(pd); } return pdc; } string ITypedList.GetListName(PropertyDescriptor[] listAccessors) { return string.Empty; } #endregion } }
WindowsApplication167/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; using Example; namespace WindowsApplication167 { public partial class Form1 : Form { public Form1() { InitializeComponent(); bool[,] matrix = new bool[10, 10]; //int[,] matrix = new int[10, 10]; //string[,] matrix = new string[10, 10]; Array2DWrapper<bool> wrapper = new Array2DWrapper<bool>(matrix); gridControl1.DataSource = wrapper; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.