using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Data;
using Telerik.Windows.Controls;
using Microsoft.Office.Interop.Excel;
using System.Dynamic;
using System.ComponentModel;

namespace RadGridView_WPF_AR_66
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : System.Windows.Window
	{
		public MainWindow()
		{
			InitializeComponent();
			Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
			List<MyDataRow> mdr = new List<MyDataRow>();
			Workbook we = xla.Workbooks.Open(@"C:\Users\iivanov\Documents\Visual Studio 2012\Projects\RadGridView_WPF_AR_66\RadGridView_WPF_AR_66\123.xlsx");
			Worksheet ws = we.Worksheets.get_Item("Sheet1");
			object[,] array = ws.Range["A1", "B3"].Value;
			for (int x = 1; x < array.GetLength(0) + 1; x++)
			{
				dynamic dyn = new MyDataRow();
				for (int y = 1; y < array.GetLength(1) + 1; y++)
				{
					dyn[y.ToString()] = array[x, y];
				}
				mdr.Add(dyn);
			}
			grid.ItemsSource = mdr;
		}
	}

	public class MyDataRow : DynamicObject, INotifyPropertyChanged
	{
		readonly IDictionary<string, object> data;

		public MyDataRow()
		{
			this.data = new Dictionary<string, object>();
		}

		public MyDataRow(IDictionary<string, object> source)
		{
			this.data = source;
		}

		public override IEnumerable<string> GetDynamicMemberNames()
		{
			return this.data.Keys;
		}

		public override bool TryGetMember(GetMemberBinder binder, out object result)
		{
			result = this[binder.Name];

			return true;
		}

		public override bool TrySetMember(SetMemberBinder binder, object value)
		{
			this[binder.Name] = value;

			return true;
		}

		public object this[string columnName]
		{
			get
			{
				if (this.data.ContainsKey(columnName))
				{
					return this.data[columnName];
				}

				return null;
			}
			set
			{
				if (!this.data.ContainsKey(columnName))
				{
					this.data.Add(columnName, value);

					this.OnPropertyChanged(columnName);
				}
				else
				{
					if (this.data[columnName] != value)
					{
						this.data[columnName] = value;

						this.OnPropertyChanged(columnName);
					}
				}
			}
		}

		private void OnPropertyChanged(string propertyName)
		{
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;
	}
}
