Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

CX Part II

, 5 Aug 2009
Build a Metadata Designer for the CX Dynamic Composition Framework.
Cx.zip
Cx
bin
Debug
Cx.Attributes.dll
Cx.Common.dll
Cx.Converters.dll
Cx.dll
Cx.EventArgs.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
Cx.vshost.exe
Cx.vshost.exe.manifest
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Settings.settings
Cx.Attributes
bin
Debug
Cx.Attributes.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.Common
bin
Debug
Cx.Attributes.dll
Cx.Common.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.ComponentLoader
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.Converters
bin
Debug
Cx.Attributes.dll
Cx.Common.dll
Cx.Converters.dll
Cx.EventArgs.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.DataService
bin
Debug
Cx.Attributes.dll
Cx.Common.dll
Cx.DataService.dll
Cx.EventArgs.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.Designer
bin
Debug
Cx.Attributes.dll
Cx.Common.dll
Cx.Designer.Common.dll
Cx.Designer.dll
Cx.Designer.Interfaces.dll
Cx.EventArgs.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.Designer.App
bin
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Debug
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.Attributes.dll
Cx.Common.dll
Cx.Designer.App.exe
Cx.Designer.App.vshost.exe
Cx.Designer.App.vshost.exe.manifest
Cx.Designer.Common.dll
Cx.Designer.Interfaces.dll
Cx.dll
Cx.EventArgs.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Settings.settings
Cx.Designer.Common
bin
Debug
Cx.Designer.Common.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.Designer.Components
bin
Debug
Cx.Attributes.dll
Cx.Common.dll
Cx.Designer.Components.dll
Cx.EventArgs.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
Cx.WinForm.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.Designer.Interfaces
bin
Debug
Cx.Designer.Interfaces.dll
Cx.Interfaces.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.EventArgs
bin
Debug
Cx.EventArgs.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.Exceptions
bin
Debug
Cx.Exceptions.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.Interfaces
bin
Debug
Cx.Interfaces.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.WinForm
bin
Debug
Cx.Interfaces.dll
Cx.WinForm.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
NumericKeypadComponent
bin
Debug
Cx.Attributes.dll
Cx.EventArgs.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
Cx.WinForm.dll
NumericKeypadComponent.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
OperatorComponent
bin
Debug
Cx.Attributes.dll
Cx.Interfaces.dll
Cx.WinForm.dll
OperatorComponent.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
TextDisplayComponent
bin
Debug
Cx.Attributes.dll
Cx.EventArgs.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
Cx.WinForm.dll
TextDisplayComponent.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
App
bin
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Debug
App.exe
App.vshost.exe
App.vshost.exe.manifest
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.Attributes.dll
Cx.Common.dll
Cx.Designer.Common.dll
Cx.Designer.dll
Cx.dll
Cx.EventArgs.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Settings.settings
articles
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
How Programming Should Be Done, Part II
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
cx2
app.jpg
editEvents.JPG
editProperties.jpg
EventTransformation.png
How Programming Should Be Done
components.vsd
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
HowProgrammingShouldBeDone
components.jpg
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
display.jpg
form.jpg
keypad.jpg
operators.jpg
BusinessUnitComponent
bin
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Debug
BusinessUnitComponent.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Cx.Attributes.dll
Cx.EventArgs.dll
Cx.Exceptions.dll
Cx.Interfaces.dll
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Cx.Attributes;
using Cx.Common;
using Cx.EventArgs;
using Cx.Exceptions;
using Cx.Interfaces;
using Cx.WinForm;

namespace Cx.Designer.Components
{
	[CxComponentName("DataGridView")]
	[CxExplicitEvent("ItemSelected")]
	[CxExplicitEvent("Data")]
	public partial class DataGridView : UserControl, ICxVisualComponentClass
	{
		protected List<ItemValue> headerText;
		protected List<ItemValue> headerWidths;
		protected List<ItemValue> dataPropertyNames;
		protected bool editable;

		[CxComponentProperty]
		public string Label
		{
			get { return label1.Text; }
			set { label1.Text = value; }
		}

		[CxComponentProperty]
		public List<ItemValue> HeaderText
		{
			get { return headerText; }
			set { headerText = value; }
		}

		[CxComponentProperty]
		public List<ItemValue> HeaderWidths
		{
			get { return headerWidths; }
			set { headerWidths = value; }
		}

		[CxComponentProperty]
		public List<ItemValue> DataPropertyNames
		{
			get { return dataPropertyNames; }
			set { dataPropertyNames = value; }
		}

		[CxComponentProperty]
		public bool Editable
		{
			get { return editable; }
			set
			{
				editable = value;

				if (editable)
				{
					EnableEdits();
				}
				else
				{
					DisableEdits();
				}
			}
		}

		protected EventHelper itemSelected;
		protected EventHelper data;

		protected BindingSource bsData;

		public DataGridView()
		{
			InitializeComponent();
			headerText = new List<ItemValue>();
			headerWidths = new List<ItemValue>();
			dataPropertyNames = new List<ItemValue>();

			Editable = false;
			dataGridView1.AllowUserToResizeRows = false;
			dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

			dataGridView1.SelectionChanged += new EventHandler(OnSelectionChanged);
			itemSelected = EventHelpers.CreateEvent<object>(this, "ItemSelected");
			data = EventHelpers.CreateEvent<IEnumerable>(this, "Data");
			bsData = new BindingSource();
		}

		protected void OnSelectionChanged(object sender, System.EventArgs e)
		{
			itemSelected.Fire(bsData.Current);
		}

		protected void EnableEdits()
		{
			dataGridView1.AllowUserToAddRows = true;
			dataGridView1.AllowUserToDeleteRows = true;
			dataGridView1.RowHeadersVisible = true;

			foreach (DataGridViewColumn dgvc in dataGridView1.Columns)
			{
				dgvc.ReadOnly = false;
			}
		}

		protected void DisableEdits()
		{
			dataGridView1.AllowUserToAddRows = false;
			dataGridView1.AllowUserToDeleteRows = false;
			dataGridView1.RowHeadersVisible = false;

			foreach (DataGridViewColumn dgvc in dataGridView1.Columns)
			{
				dgvc.ReadOnly = false;
			}
		}

		[CxConsumer]
		public void OnData(object sender, CxEventArgs<IEnumerable> args)
		{
			bsData.DataSource = args.Data;

			if (headerText.Count == 0)
			{
				dataGridView1.AutoGenerateColumns = true;
			}
			else
			{
				dataGridView1.AutoGenerateColumns = false;
				InitializeColumns();
			}

			dataGridView1.DataSource = bsData;
			dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Ascending);
		}

		/// <summary>
		/// Without data binding, we have a more complicated process for sending data to the model.
		/// </summary>
		[CxConsumer]
		public void OnRequestData(object sender, System.EventArgs args)
		{
			data.Fire(bsData.DataSource);
		}

		public void Register(object form, ICxVisualComponent comp)
		{
			this.RegisterControl((Form)form, comp);
		}

		protected void InitializeColumns()
		{
			dataGridView1.Columns.Clear();
			bool useWidths = false;

			if (headerWidths.Count != 0)
			{
				Verify.IsTrue(headerText.Count == headerWidths.Count, "Header width item count does not match header text item count.");
				useWidths = true;
			}

			for (int i = 0; i < headerText.Count; i++)
			{
				DataGridViewTextBoxColumn dgvc = new DataGridViewTextBoxColumn();
				dgvc.HeaderText = headerText[i].Text;
				dgvc.DataPropertyName = dataPropertyNames[i].Text;
				dgvc.ReadOnly = !editable;

				if (useWidths)
				{
					dgvc.Width = Convert.ToInt32(headerWidths[i].Text);
				}

				dataGridView1.Columns.Add(dgvc);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Marc Clifton

United States United States
Marc is the creator of two open source projets, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.
 
Marc lives in Philmont, NY.

| Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 5 Aug 2009
Article Copyright 2009 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid