DataGridView Custom ComboBox Column continued…

I found some code here that I was able to modify to accomplish what I wanted:

http://www.koders.com/csharp/fid2E788846AF503E85D1702ABB02A0434174BDD7BA.aspx?s=datagridview

Here’s what it kind of looks like in the interim:

and here’s the code with my modifications:

/**************************************************************************************************
 * 
 * FILE NAME:
 *      DataGridViewMultiColumnComboColumn.cs
 * 
 * AUTHOR:
 *      Issahar Gourfinkel, 
 *      gurfi@barak-online.net
 *      This code is Completely free. I will be happy if it will help somebody. 
 *      
 * CHANGES:
 *		Steven J. Ackerman
 *		sjackerman@verizon.net
 *		Made OwnerDrawVariable, DropDownList style
 *		Added OnMeasureItem(), OnDataSourceChanged(), OnValueMemberChanged()
 *		Changed OnDrawItem() to paint similiar to MultiColumnComboBox
 * 
 * DESCRIPTION:
 *      MultiColumnCombobox simulation for DataGridView cells controls.
 * 
 * DISCLAIMER OF WARRANTY:
 *      All of the code, information, instructions, and recommendations in this software component are 
 *      offered on a strictly "as is" basis. This material is offered as a free public resource, 
 *      without any warranty, expressed or implied.
 */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

public class DataGridViewMultiColumnComboColumn : DataGridViewComboBoxColumn
{
    public DataGridViewMultiColumnComboColumn()
    {
        //Set the type used in the DataGridView
        this.CellTemplate = new DataGridViewMultiColumnComboCell();
    }
}
public class DataGridViewMultiColumnComboCell : DataGridViewComboBoxCell
{
    public override Type EditType
    {
        get
        {
            return typeof(DataGridViewMultiColumnComboEditingControl);
        }
    }
    public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
        DataGridViewMultiColumnComboEditingControl ctrl = DataGridView.EditingControl as DataGridViewMultiColumnComboEditingControl;
        ctrl.ownerCell = this;
    }
}
public class DataGridViewMultiColumnComboEditingControl : DataGridViewComboBoxEditingControl
{
	const int columnPadding = 5;
	private const int colorColumnWidth = 100;
	private float[] columnWidths = new float[0];
	private String[] columnNames = new String[0];
	private Type[] columnTypes = new Type[0];
	private int valueMemberColumnIndex = 0;

    public DataGridViewMultiColumnComboCell ownerCell = null;
    /**************************************************************************************************/
    public DataGridViewMultiColumnComboEditingControl()
        : base()
    {
        this.DrawMode = DrawMode.OwnerDrawVariable;
        this.DropDownStyle = ComboBoxStyle.DropDownList;
    }

	private void InitializeColumns()
	{
		if (DataManager != null)
		{
			PropertyDescriptorCollection propertyDescriptorCollection = DataManager.GetItemProperties();

			columnWidths = new float[propertyDescriptorCollection.Count];
			columnNames = new String[propertyDescriptorCollection.Count];
			columnTypes = new Type[propertyDescriptorCollection.Count];

			for (int colIndex = 0; colIndex < propertyDescriptorCollection.Count; colIndex++)
			{
				String name = propertyDescriptorCollection[colIndex].Name;
				Type type = propertyDescriptorCollection[colIndex].PropertyType;
				columnNames[colIndex] = name;
				columnTypes[colIndex] = type;
			}
		}
	}

	private void InitializeValueMemberColumn()
	{
		int colIndex = 0;
		foreach (String columnName in columnNames)
		{
			if (String.Compare(columnName, ValueMember, true, CultureInfo.CurrentUICulture) == 0)
			{
				valueMemberColumnIndex = colIndex;
				break;
			}
			colIndex++;
		}
	}

	private float CalculateTotalWidth()
	{
		float totWidth = 0;
		foreach (int width in columnWidths)
		{
			totWidth += (width + columnPadding);
		}
		return totWidth + SystemInformation.VerticalScrollBarWidth;
	}

	protected override void OnDataSourceChanged(EventArgs e)
	{
		base.OnDataSourceChanged(e);

		InitializeColumns();
	}

	protected override void OnValueMemberChanged(EventArgs e)
	{
		base.OnValueMemberChanged(e);

		InitializeValueMemberColumn();
	}

	protected override void OnDropDown(EventArgs e)
	{
		base.OnDropDown(e);
		this.DropDownWidth = (int)CalculateTotalWidth();
	}

	protected override void OnMeasureItem(MeasureItemEventArgs e)
	{
		base.OnMeasureItem(e);

		if (DesignMode)
			return;

		for (int colIndex = 0; colIndex < columnNames.Length; colIndex++)
		{
			if (columnTypes[colIndex] == typeof(Color))
			{
				columnWidths[colIndex] = colorColumnWidth;
			}
			else
			{
				string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], columnNames[colIndex]));
				SizeF sizeF = e.Graphics.MeasureString(item, Font);
				columnWidths[colIndex] = Math.Max(columnWidths[colIndex], sizeF.Width);
			}
		}

		float totWidth = CalculateTotalWidth();

		e.ItemWidth = (int)totWidth;
	}

	/**************************************************************************************************/
    protected override void OnDrawItem(DrawItemEventArgs e)
    {

		base.OnDrawItem(e);

		if (DesignMode)
			return;

		e.DrawBackground();

		Rectangle boundsRect = e.Bounds;
		int lastRight = 0;

		using (Pen linePen = new Pen(SystemColors.GrayText))
		{
			using (SolidBrush forecolorBrush = new SolidBrush(e.ForeColor))
			{
				Brush brush;

				if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
				{
					brush = SystemBrushes.HighlightText;
				}
				else
				{
					brush = forecolorBrush;
				}

				if (columnNames.Length == 0)
				{
					e.Graphics.DrawString(Convert.ToString(Items[e.Index]), Font, brush, boundsRect);
				}
				else
				{
					for (int colIndex = 0; colIndex < columnNames.Length; colIndex++)
					{
						boundsRect.X = lastRight;
						boundsRect.Width = (int)columnWidths[colIndex] + columnPadding;
						lastRight = boundsRect.Right;

						if (columnTypes[colIndex] == typeof(Color))
						{
							const int distanceFromEdge = 2;
							boundsRect.Width = colorColumnWidth;
							lastRight = boundsRect.Right;
							Rectangle colorBoxRect = new Rectangle();
							colorBoxRect.X = boundsRect.X + distanceFromEdge;
							colorBoxRect.Y = boundsRect.Y + 1;
							colorBoxRect.Size = new Size((int)(1.5 * 17), boundsRect.Height - (2 * distanceFromEdge));
							RectangleF textBoxRect = new RectangleF();
							textBoxRect = RectangleF.FromLTRB(colorBoxRect.X + colorBoxRect.Width + 5, colorBoxRect.Y,
															  boundsRect.X + boundsRect.Width - distanceFromEdge,
															  colorBoxRect.Y + colorBoxRect.Height + 2);
							if (e.Index >= 0)
							{
								Color color = (Color) FilterItemOnProperty(Items[e.Index], columnNames[colIndex]);
								using (SolidBrush background = new SolidBrush(color))
								{
									e.Graphics.FillRectangle(background, colorBoxRect);
									e.Graphics.DrawRectangle(linePen, colorBoxRect);
									e.Graphics.DrawString("0x" + color.Name.ToString(), this.Font, brush, textBoxRect);
								}
							}
						}
						else
						{
							if (e.Index >= 0)
							{
								string item = Convert.ToString(FilterItemOnProperty(Items[e.Index], columnNames[colIndex]));

								if (colIndex == valueMemberColumnIndex)
								{
									using (Font boldFont = new Font(Font, FontStyle.Bold))
									{
										e.Graphics.DrawString(item, boldFont, brush, boundsRect);
									}
								}
								else
								{
									e.Graphics.DrawString(item, Font, brush, boundsRect);
								}
							}
						}

						if (colIndex < columnNames.Length - 1)
						{
							e.Graphics.DrawLine(linePen, boundsRect.Right, boundsRect.Top, boundsRect.Right, boundsRect.Bottom);
						}
					}
				}
			}
		}

		e.DrawFocusRectangle();
    }
    /**************************************************************************************************/
}


And here’s the form code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Acs;
using Backgrounder;

namespace ColorLcdDisplayTest
{
	public partial class EditDisplaySchemesDialog : Form
	{
		private AcsColorLcd320X240 _display;
		private BindingSource _schemesBindingSource = new BindingSource();
		private BindingSource _fontsBindingSource = new BindingSource();
		private const int _fontColumnFixedWidth = 100;
		private const int _colorColumnFixedWidth = 100;

		public EditDisplaySchemesDialog(AcsColorLcd320X240 display)
		{
			_display = display;

			InitializeComponent();

			_fontsBindingSource.DataSource = display.Fonts;
			dataGridViewSchemes.AutoGenerateColumns = false;

			// col 0
			DataGridViewTextBoxColumn textBoxColumn = new DataGridViewTextBoxColumn();
			textBoxColumn.Name = "Number";
			textBoxColumn.DataPropertyName = "SchemeNumber";
			textBoxColumn.HeaderText = "#";
			dataGridViewSchemes.Columns.Add(textBoxColumn);

			// col 1
			ColorPickerColumn colorPickerColumn = new ColorPickerColumn();
			colorPickerColumn.Name = "BlkReplColor";
			colorPickerColumn.DataPropertyName = "BlkReplColor";
			colorPickerColumn.HeaderText = "Replace Black";
			dataGridViewSchemes.Columns.Add(colorPickerColumn);

			// col 2
			colorPickerColumn = new ColorPickerColumn();
			colorPickerColumn.Name = "WhtReplColor";
			colorPickerColumn.DataPropertyName = "WhtReplColor";
			colorPickerColumn.HeaderText = "Replace White";
			dataGridViewSchemes.Columns.Add(colorPickerColumn);

			// col 3
			DataGridViewMultiColumnComboColumn fontPickerColumn = new DataGridViewMultiColumnComboColumn();
			fontPickerColumn.Name = "FontNumber";
			fontPickerColumn.DataPropertyName = "FontNumber";
			fontPickerColumn.DataSource = _fontsBindingSource;
			fontPickerColumn.DisplayMember = "Name";
			fontPickerColumn.ValueMember = "FontNumber";
			fontPickerColumn.HeaderText = "#-Font";
			dataGridViewSchemes.Columns.Add(fontPickerColumn);

			// col 4
			DataGridViewComboBoxColumn comboBoxColumn = new DataGridViewComboBoxColumn();
			comboBoxColumn.Name = "Colorize";
			comboBoxColumn.DataPropertyName = "Colorize";
			comboBoxColumn.DataSource = Enum.GetValues(typeof(AcsColorLcd320X240.IconColorize));
			comboBoxColumn.ValueType = typeof(AcsColorLcd320X240.IconColorize);
			comboBoxColumn.HeaderText = "Colorize";
			dataGridViewSchemes.Columns.Add(comboBoxColumn);

			// col 5
			textBoxColumn = new DataGridViewTextBoxColumn();
			textBoxColumn.Name = "TextPos";
			textBoxColumn.DataPropertyName = "TextPos";
			textBoxColumn.HeaderText = "Text Pos";
			dataGridViewSchemes.Columns.Add(textBoxColumn);

			// col 6
			comboBoxColumn = new DataGridViewComboBoxColumn();
			comboBoxColumn.Name = "IconTransp";
			comboBoxColumn.DataPropertyName = "IconTransp";
			comboBoxColumn.DataSource = Enum.GetValues(typeof(AcsColorLcd320X240.BitmapTransparencyMode));
			comboBoxColumn.ValueType = typeof(AcsColorLcd320X240.BitmapTransparencyMode);
			comboBoxColumn.HeaderText = "Transparency";
			dataGridViewSchemes.Columns.Add(comboBoxColumn);
		}

		private void refreshDataGrid()
		{
			_schemesBindingSource.DataSource = _display.Schemes;
			dataGridViewSchemes.DataSource = _schemesBindingSource;

			dataGridViewSchemes.AutoResizeColumns();

			dataGridViewSchemes.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
			dataGridViewSchemes.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			dataGridViewSchemes.Columns[1].Width = _colorColumnFixedWidth;
			dataGridViewSchemes.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			dataGridViewSchemes.Columns[2].Width = _colorColumnFixedWidth;
			dataGridViewSchemes.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
			dataGridViewSchemes.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
		}

		private void bindFonts()
		{
			_fontsBindingSource.DataSource = _display.Fonts;
		}

		private delegate void SetCommandResponseTimeoutCallback(object source, AcsColorLcd320X240.CommandResponse response);
		void _display_CommandResponseEvent(object source, AcsColorLcd320X240.CommandResponse commandResponse)
		{
			if (buttonReceive.InvokeRequired)
			{
				SetCommandResponseTimeoutCallback callback = new SetCommandResponseTimeoutCallback(_display_CommandResponseEvent);
				this.Invoke(callback, new object[] { source, commandResponse });
			}
			else
			{
				if (commandResponse != AcsColorLcd320X240.CommandResponse.CommandOk)
				{
					if (_display.Fonts.Count == 0)
					{
						_display.Fonts.AddNew();
					}
					if (_display.Schemes.Count == 0)
					{
						_display.Schemes.AddNew();
					}
					refreshDataGrid();
				}
				_display.CommandResponseEvent -= _display_CommandResponseEvent;
				buttonReceive.Enabled = true;
			}
		}

		private delegate void SetGridDataSourceCallback(object source);
		void _display_QuerySchemeItemResponseDoneEvent(object source)
		{
			if (dataGridViewSchemes.InvokeRequired)
			{
				SetGridDataSourceCallback callback = new SetGridDataSourceCallback(_display_QuerySchemeItemResponseDoneEvent);
				this.Invoke(callback, new object[] { source });
			}
			else
			{
				if (_display.Fonts.Count == 0)
				{
					_display.Fonts.AddNew();
				}
				refreshDataGrid();
				_display.QuerySchemeResponseDoneEvent -= _display_QuerySchemeItemResponseDoneEvent;
				_display.CommandResponseEvent -= _display_CommandResponseEvent;
				buttonReceive.Enabled = true;
			}
		}

		private void DisplaySchemesDialog_Shown(object sender, EventArgs e)
		{
			_display.ResetEvent += new AcsColorLcd320X240.ResetEventHandler(_display_ResetEvent);
			if (_display.Fonts.Count == 0)
			{
				_display.Fonts.AddNew();
			}

			if (_display.Schemes.Count == 0)
			{
				_display.Schemes.AddNew();
			}

			refreshDataGrid();
		}

		private void EnableControls(bool enable)
		{
			buttonLoadFonts.Enabled = enable;
			buttonSaveFonts.Enabled = enable;
			buttonReceive.Enabled = enable;
			buttonSend.Enabled = enable;
		}

		private delegate void BindFonts(object source);
		private void _display_QueryFontItemResponseDoneEvent(object source)
		{
			if (dataGridViewSchemes.InvokeRequired)
			{
				BindFonts callback = new BindFonts(_display_QueryFontItemResponseDoneEvent);
				this.Invoke(callback, new object[] {source});
			}
			else
			{
				bindFonts();
				_display.QueryFontItemResponseDoneEvent -= _display_QueryFontItemResponseDoneEvent;
				_display.CommandResponseEvent -= _display_CommandResponseEvent;
				refreshDataGrid();
			}
		}

		private delegate void CloseForm(object source);
		void _display_ResetEvent(object source)
		{
			if (this.InvokeRequired)
			{
				CloseForm callback = new CloseForm(_display_ResetEvent);
				this.BeginInvoke(callback, new object[]{source});
			}
			else
			{
				if (this.IsDisposed) return;
				if (!this.IsHandleCreated) return;

				DialogResult = DialogResult.Cancel;
				Close();
			}
		}

		private void EditDisplaySchemesDialog_FormClosing(object sender, FormClosingEventArgs e)
		{
			_display.ResetEvent -= _display_ResetEvent;

			if (Properties.Settings.Default.EditDisplaySchemesFormPosition == null)
			{
				Properties.Settings.Default.EditDisplaySchemesFormPosition = new WindowSettings();
			}
			Properties.Settings.Default.EditDisplaySchemesFormPosition.Record(this);
			Properties.Settings.Default.Save();
		}

		private void DisplaySchemesDialog_Load(object sender, EventArgs e)
		{
			if (Properties.Settings.Default.EditDisplaySchemesFormPosition != null)
			{
				Properties.Settings.Default.EditDisplaySchemesFormPosition.Restore(this);
			}
			else
			{
				this.Location = this.Owner.Location + (this.Owner.Size - this.Size);
			}
		}

		private void dataGridViewSchemes_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			DataGridView dataGridView = sender as DataGridView;

			if (dataGridView != null)
			{
				ColorDialog colorDialog = new ColorDialog();

				if (e.RowIndex < 0) 					return; 				AcsColorLcd320X240.Scheme scheme = dataGridView.Rows[e.RowIndex].DataBoundItem as AcsColorLcd320X240.Scheme; 				DataGridViewComboBoxCell comboBoxCell; 				DataGridViewComboBoxEditingControl comboBoxEditingControl; 				switch (e.ColumnIndex) 				{ 					case 1: 						colorDialog.Color = scheme.BlkReplColor; 						if (colorDialog.ShowDialog() == DialogResult.OK) 						{ 							scheme.BlkReplColor = colorDialog.Color; 							_display.SchemeColor((byte)e.RowIndex, scheme.BlkReplColor, scheme.WhtReplColor); 						} 						dataGridViewSchemes.InvalidateCell(e.ColumnIndex, e.RowIndex); 						break; 					case 2: 						colorDialog.Color = scheme.WhtReplColor; 						if (colorDialog.ShowDialog() == DialogResult.OK) 						{ 							scheme.WhtReplColor = colorDialog.Color; 							_display.SchemeColor((byte)e.RowIndex, scheme.BlkReplColor, scheme.WhtReplColor); 						} 						dataGridViewSchemes.InvalidateCell(e.ColumnIndex, e.RowIndex); 						break; 					case 5: 						EditDisplaySchemeTextPositionDialog schemeTextPositionDialog = new EditDisplaySchemeTextPositionDialog(); 						schemeTextPositionDialog.TextPosition = scheme.TextPos; 						schemeTextPositionDialog.StartPosition = FormStartPosition.Manual; 						if (schemeTextPositionDialog.ShowDialog(this) == DialogResult.OK) 						{ 							scheme.TextPos = schemeTextPositionDialog.TextPosition; 							_display.SchemeAttributes((byte)e.RowIndex, (byte)scheme.FontNumber, scheme.Colorize, scheme.TextPos, scheme.IconTransp); 							dataGridViewSchemes.InvalidateCell(e.ColumnIndex, e.RowIndex); 						} 						break; 					case 3: 					case 4: 					case 6: 						dataGridViewSchemes.BeginEdit(true); 						comboBoxCell = (DataGridViewComboBoxCell)dataGridViewSchemes[e.ColumnIndex, e.RowIndex]; 						dataGridViewSchemes.CurrentCell = comboBoxCell; 						comboBoxEditingControl = (DataGridViewComboBoxEditingControl)dataGridViewSchemes.EditingControl; 						comboBoxEditingControl.DroppedDown = true; 						break; 					default: 						break; 				} 			} 		} 		private void dataGridViewSchemes_CellValueChanged(object sender, DataGridViewCellEventArgs e) 		{ 			DataGridView dataGridView = sender as DataGridView; 			if (dataGridView != null) 			{ 				AcsColorLcd320X240.Scheme scheme = dataGridView.Rows[e.RowIndex].DataBoundItem as AcsColorLcd320X240.Scheme; 				switch (e.ColumnIndex) 				{ 					case 1: 					case 2: 						_display.SchemeColor((byte)scheme.SchemeNumber, scheme.BlkReplColor, scheme.WhtReplColor); 						break; 					case 3: 					case 4: 					case 6: 						dataGridViewSchemes.EndEdit(); 						_display.SchemeAttributes((byte)scheme.SchemeNumber, (byte)scheme.FontNumber, scheme.Colorize, scheme.TextPos, scheme.IconTransp); 						break; 					default: 						break; 				} 			} 		} 		private void buttonSaveSchemes_Click(object sender, EventArgs e) 		{ 			SaveFileDialog saveSchemes = new SaveFileDialog(); 			saveSchemes.Filter = "LCD schemes (*.schemes)|*.schemes|All files (*.*)|*.*"; 			if (saveSchemes.ShowDialog() == DialogResult.OK) 			{ 				using (StreamWriter sWriter = File.CreateText(saveSchemes.FileName)) 				{ 					string schemes = _display.SchemesToJSON(); 					sWriter.Write(schemes); 				} 			} 		} 		private void buttonLoadSchemes_Click(object sender, EventArgs e) 		{ 			OpenFileDialog loadSchemes = new OpenFileDialog(); 			loadSchemes.Filter = "LCD schemes (*.schemes)|*.schemes|All files (*.*)|*.*"; 			loadSchemes.CheckFileExists = true; 			if (loadSchemes.ShowDialog() == DialogResult.OK) 			{ 				using (StreamReader sReader = File.OpenText(loadSchemes.FileName)) 				{ 					string schemes = sReader.ReadToEnd(); 					_display.SchemesFromJSON(schemes); 					if (_display.Fonts.Count == 0) 					{ 						_display.Fonts.AddNew(); 					} 					refreshDataGrid(); 				} 			} 		} 		private ManualResetEvent manualResetEvent = new ManualResetEvent(false); 		private AcsColorLcd320X240.CommandResponse _response; 		private void ResetResponseWait() 		{ 			_response = AcsColorLcd320X240.CommandResponse.None; 			manualResetEvent.Reset(); 		} 		private bool AckResponseReceived() 		{ 			manualResetEvent.WaitOne(); 			return _response == AcsColorLcd320X240.CommandResponse.CommandOk; 		} 		void _display_WaitForCommandResponseEvent(object source, AcsColorLcd320X240.CommandResponse commandResponse) 		{ 			_response = commandResponse; 			manualResetEvent.Set(); 		} 		private BackgroundHelper backgroundHelper = new BackgroundHelper(); 		private void buttonSend_Click(object sender, EventArgs e) 		{ 			EnableControls(false); 			backgroundHelper.Background(() =>
			{
			    _display.CommandResponseEvent += new AcsColorLcd320X240.CommandResponseEventHandler(_display_WaitForCommandResponseEvent);

			    foreach (AcsColorLcd320X240.Scheme scheme in _display.Schemes)
			    {
			        ResetResponseWait();
			        _display.SchemeColor((byte) scheme.SchemeNumber, scheme.BlkReplColor,
			                            	scheme.WhtReplColor);
			        if (!AckResponseReceived())
			            goto exit;

			        ResetResponseWait();
			        _display.SchemeAttributes((byte) scheme.SchemeNumber, (byte) scheme.FontNumber,
			                            		scheme.Colorize, scheme.TextPos, scheme.IconTransp);
			        if (!AckResponseReceived())
			            goto exit;
			    }

			    exit:
					_display.CommandResponseEvent -= _display_WaitForCommandResponseEvent;
			});

			EnableControls(true);
		}

		private void buttonReceive_Click(object sender, EventArgs e)
		{
			buttonReceive.Enabled = false;
			if (_display != null)
			{
				_display.QuerySchemeResponseDoneEvent += new AcsColorLcd320X240.QuerySchemeResponseDoneEventHandler(_display_QuerySchemeItemResponseDoneEvent);
				_display.CommandResponseEvent += new AcsColorLcd320X240.CommandResponseEventHandler(_display_CommandResponseEvent);
				_display.QuerySchemes();
			}
		}

		private void dataGridViewSchemes_DataError(object sender, DataGridViewDataErrorEventArgs e)
		{
			/* prevent user from saving/sending bad data */
			buttonSend.Enabled = false;
			buttonSaveFonts.Enabled = false;

			DataGridViewCellStyle errorStyle = new DataGridViewCellStyle();
			errorStyle.BackColor = Color.Yellow;
			dataGridViewSchemes.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = errorStyle;

			e.Cancel = true;
		}

		private void dataGridViewSchemes_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			DataGridView dataGridView = sender as DataGridView;
			StringBuilder sb = new StringBuilder();

			if (dataGridView != null)
			{
				AcsColorLcd320X240.Scheme scheme = dataGridView.Rows[e.RowIndex].DataBoundItem as AcsColorLcd320X240.Scheme;

				// double row highlighting...
				if ((scheme.SchemeNumber & 2) != 0)
				{
					dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.FromArgb(192, 255, 255);
				}

				// font # column on/off labeling...
				if (e.RowIndex >= 0 && e.ColumnIndex == 0)
				{
					if ((scheme.SchemeNumber & 1) != 0)
					{
						e.Value = sb.AppendFormat("{0}-on", scheme.SchemeNumber);
					}
					else
					{
						e.Value = sb.AppendFormat("{0}-off", scheme.SchemeNumber);
					}
				}

				// custom font labeling
				if (e.RowIndex >= 0 && e.ColumnIndex == 3)
				{
					e.Value = sb.AppendFormat("{0}-{1}", scheme.FontNumber, e.Value);
				}

				// custom text pos labeling
				if (e.RowIndex >= 0 && e.ColumnIndex == 5)
				{
					ushort value, valueX, valueY;

					value = ushort.Parse(e.Value.ToString());

					valueX = (ushort)((value >> 6) * 16);
					valueY = (ushort)(((value >> 2) & 15) * 16);

					switch (value)
					{
						case 0:
							e.Value = sb.AppendFormat("{0}-No Text", value);
							break;

						case 1:
							e.Value = sb.AppendFormat("{0}-Centered", value);
							break;

						default:
							e.Value = sb.AppendFormat("{0}-({1}%,{2}%)", value, valueX, valueY);
							break;
					}
				}
			}
		}
	}
}

Advertisements
Post a comment or leave a trackback: Trackback URL.

Comments

  • koze  On 1-August-13 at 9:55 pm

    hello is it possible to include also the form code for the screenshot thanks a lot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: