Figure 1:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinListView;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
/// <summary>
/// Provides access to the codeset for my db.
/// </summary>
[ProvideProperty("Codeset", typeof(Control))]
public partial class CodesetProvider : Component, IExtenderProvider
{
private const string FILTER_CODESET = "CODESET_ID = {0}";
private const string TABLE_CODESET_DETAILS = "TABLE_CODESET_DETAILS";
private const string DATABASE =
"myDB";
private const string SELECT_CODESET_DETAILS = "SELECT * FROM CODESETS_DETAIL";
private const string COLUMN_DATA_VALUE = "DATA_VALUE";
private const string COLUMN_DISPLAY_VALUE = "DISPLAY_VALUE";
private const string NONE_SELECTED = "None Selected";
private CacheManager _cache;
private List<ControlReferences> _codesetNumbers = new List<ControlReferences>();
/// <summary>
/// Initializes a new instance of the <see cref="CodesetProvider"/> class.
/// </summary>
public CodesetProvider()
{
InitializeComponent();
}
/// <summary>
/// Initializes a new instance of the <see cref="CodesetProvider"/> class.
/// </summary>
/// <param name="container">The container.</param>
public CodesetProvider(IContainer container)
{
container.Add(this);
InitializeComponent();
}
#region IExtenderProvider Members
/// <summary>
/// Specifies whether this object can provide its extender properties to the specified object.
/// </summary>
/// <param name="extendee">The <see cref="T:System.Object"></see> to receive the extender properties.</param>
/// <returns>
/// true if this object can provide extender properties to the specified object; otherwise, false.
/// </returns>
public bool CanExtend(object extendee)
{
if (extendee is UltraComboEditor || extendee is UltraListView
|| extendee is UltraDropDown || extendee is UltraCombo
|| extendee is UltraGrid)
{
return true;
}
return false;
}
#endregion
/// <summary>
/// Gets the codeset number.
/// </summary>
/// <param name="control">The control.</param>
/// <returns></returns>
public CodesetProperty GetCodeset(Control control)
{
ControlReferences reference = new ControlReferences(control);
int value = _codesetNumbers.IndexOf(reference);
if (value != -1)
{
return _codesetNumbers[value].Codeset;
}
return new CodesetProperty(0, NONE_SELECTED);
}
/// <summary>
/// Sets the codeset number.
/// </summary>
/// <param name="control">The control.</param>
/// <param name="codeset">The codeset.</param>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public void SetCodeset(Control control, CodesetProperty codeset)
{
ControlReferences reference = new ControlReferences(control, codeset);
int value = _codesetNumbers.IndexOf(reference);
if (value != -1)
{
_codesetNumbers[value] = reference;
return;
}
else
{
_codesetNumbers.Add(reference);
return;
}
}
public void DataBind()
{
_cache = CacheFactory.GetCacheManager();
DataTable table = _cache.GetData(TABLE_CODESET_DETAILS) as DataTable;
if (table == null)
{
Database data = DatabaseFactory.CreateDatabase(DATABASE);
DataSet set = data.ExecuteDataSet(CommandType.Text, SELECT_CODESET_DETAILS);
table = set.Tables[0];
_cache.Add(TABLE_CODESET_DETAILS, table);
}
foreach (ControlReferences reference in _codesetNumbers)
{
if (reference.Codeset != null && reference.Codeset.Number > 1)
{
DataView view = new DataView(table);
view.RowFilter = string.Format(FILTER_CODESET, reference.Codeset.Number);
view.Sort = COLUMN_DISPLAY_VALUE;
if (view.Count > 0)
{
BindControl(reference.Control, view);
}
}
}
}
public void DataBind(Control control, int codeSetNumber)
{
if (codeSetNumber < 1)
{
_cache = CacheFactory.GetCacheManager();
DataTable table = _cache.GetData(TABLE_CODESET_DETAILS) as DataTable;
if (table == null)
{
Database data = DatabaseFactory.CreateDatabase(DATABASE);
DataSet set = data.ExecuteDataSet(CommandType.Text, SELECT_CODESET_DETAILS);
table = set.Tables[0];
_cache.Add(TABLE_CODESET_DETAILS, table);
}
DataView view = new DataView(table);
view.RowFilter = string.Format(FILTER_CODESET, codeSetNumber);
BindControl(control, view);
}
}
private void BindControl(Control control, DataView view)
{
UltraComboEditor editor = control as UltraComboEditor;
if (editor != null)
{
editor.Items.Clear();
editor.DataSource = null;
editor.DisplayMember = COLUMN_DISPLAY_VALUE;
editor.ValueMember = COLUMN_DATA_VALUE;
editor.SetDataBinding(view, "");
return;
}
UltraCombo combo = control as UltraCombo;
if (combo != null)
{
combo.DataSource = null;
combo.DisplayMember = COLUMN_DISPLAY_VALUE;
combo.ValueMember = COLUMN_DATA_VALUE;
combo.SetDataBinding(view, "");
return;
}
UltraListView listview = control as UltraListView;
if (listview != null)
{
listview.Items.Clear();
foreach (DataRowView row in view)
{
listview.Items.Add(row[COLUMN_DATA_VALUE].ToString(), row[COLUMN_DISPLAY_VALUE]);
}
return;
}
UltraDropDown dropDown = control as UltraDropDown;
if (dropDown != null)
{
dropDown.DataSource = null;
dropDown.DisplayMember = COLUMN_DISPLAY_VALUE;
dropDown.ValueMember = COLUMN_DATA_VALUE;
dropDown.SetDataBinding(view, "");
}
}
}
}
Figure 2:
using System.ComponentModel;
using System.Drawing.Design;
/// <summary>
/// The property exposed by the codeset provider for codesets.
/// </summary>
[TypeConverter(typeof (CodesetTypeConverter))]
[Editor(typeof (CodesetTypeEditor), typeof (UITypeEditor))]
public class CodesetProperty
{
private int _codesetNumber;
private string _codesetName;
/// <summary>
/// Gets or sets the number.
/// </summary>
/// <value>The number.</value>
[Browsable(true)]
[ReadOnly(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int Number
{
get { return _codesetNumber; }
set { _codesetNumber = value; }
}
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[Browsable(true)]
[ReadOnly(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string Name
{
get { return _codesetName; }
set { _codesetName = value; }
}
/// <summary>
/// Initializes a new instance of the <see cref="Mps.Carolina.UI.Controls.CodesetProperty"/> class.
/// </summary>
/// <param name="codesetNumber">The codeset number.</param>
/// <param name="codesetName">Name of the codeset.</param>
public CodesetProperty(int codesetNumber, string codesetName)
{
_codesetName = codesetName;
_codesetNumber = codesetNumber;
}
}
Figure 3:
using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;
namespace Mps.Carolina.UI.Controls
{
/// <summary>
/// Type converter used by the codeset provider.
/// </summary>
public class CodesetTypeConverter : ExpandableObjectConverter
{
/// <summary>
/// Returns whether this converter can convert the object to the specified type, using the specified context.
/// </summary>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
/// <param name="destinationType">A <see cref="T:System.Type"></see> that represents the type you want to convert to.</param>
/// <returns>
/// true if this converter can perform the conversion; otherwise, false.
/// </returns>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if ( destinationType == typeof(InstanceDescriptor))
{
return true;
}
return false;
}
/// <summary>
/// Converts the given value object to the specified type, using the specified context and culture information.
/// </summary>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
/// <param name="culture">A <see cref="T:System.Globalization.CultureInfo"></see>. If null is passed, the current culture is assumed.</param>
/// <param name="value">The <see cref="T:System.Object"></see> to convert.</param>
/// <param name="destinationType">The <see cref="T:System.Type"></see> to convert the value parameter to.</param>
/// <returns>
/// An <see cref="T:System.Object"></see> that represents the converted value.
/// </returns>
/// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception>
/// <exception cref="T:System.ArgumentNullException">The destinationType parameter is null. </exception>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value,
Type destinationType)
{
CodesetProperty prop = value as CodesetProperty;
if (prop != null)
{
if (destinationType == typeof(string))
{
return prop.Name;
}
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(CodesetProperty).GetConstructor(new Type[] { typeof(int), typeof(string) });
InstanceDescriptor desc = new InstanceDescriptor(ci, new Object[] { prop.Number, prop.Name });
return desc;
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
Figure 4:
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using Infragistics.Win;
using Infragistics.Win.UltraWinListView;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
public class CodesetTypeEditor : UITypeEditor
{
private IWindowsFormsEditorService wfes;
/// <summary>
/// Gets the editor style used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"></see> method.
/// </summary>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
/// <returns>
/// A <see cref="T:System.Drawing.Design.UITypeEditorEditStyle"></see> value that indicates the style of editor used by the <see cref="M:System.Drawing.Design.UITypeEditor.EditValue(System.IServiceProvider,System.Object)"></see> method. If the <see cref="T:System.Drawing.Design.UITypeEditor"></see> does not support this method, then <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> will return <see cref="F:System.Drawing.Design.UITypeEditorEditStyle.None"></see>.
/// </returns>
public override UITypeEditorEditStyle
GetEditStyle(ITypeDescriptorContext context)
{
if (context != null && context.Instance != null)
{
return UITypeEditorEditStyle.DropDown;
}
return base.GetEditStyle(context);
}
/// <summary>
/// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> method.
/// </summary>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
/// <param name="provider">An <see cref="T:System.IServiceProvider"></see> that this editor can use to obtain services.</param>
/// <param name="value">The object to edit.</param>
/// <returns>
/// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
/// </returns>
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
wfes = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if ((wfes == null) || (context == null))
{
return null;
}
DataTable table = null;
SqlDatabase db = new SqlDatabase(CodesetRegistry.GetConnectionString());
DataSet set = db.ExecuteDataSet(CommandType.Text, "SELECT * FROM GENERAL.CODESETS");
if (set != null && set.Tables.Count > 0)
{
table = set.Tables[0];
}
if (table != null)
{
CodesetProperty prop = value as CodesetProperty;
UltraListView editor = new UltraListView();
editor.ItemSettings.AllowEdit = DefaultableBoolean.False;
editor.ItemSettings.SelectionType = SelectionType.Single;
editor.View = UltraListViewStyle.List;
editor.ViewSettingsDetails.AllowColumnMoving = false;
editor.ViewSettingsDetails.AllowColumnSizing = false;
editor.ViewSettingsList.ImageSize = new Size(0, 0);
editor.ViewSettingsList.MultiColumn = false;
editor.Size = new Size(400, 150);
UltraListViewItem previousValue = null;
foreach (DataRow row in table.Rows)
{
UltraListViewItem item = new UltraListViewItem();
item.Value = row["CODESET_NAME"];
item.Tag = row["ID"];
if (prop != null && (int)row["ID"] == prop.Number)
{
previousValue = item;
}
editor.Items.Add(item);
}
if (previousValue != null)
{
editor.SelectedItems.Add(previousValue);
}
editor.ItemDoubleClick += new ItemDoubleClickEventHandler(editor_ItemDoubleClick);
wfes.DropDownControl(editor);
editor.ItemDoubleClick -= new ItemDoubleClickEventHandler(editor_ItemDoubleClick);
if (editor.SelectedItems != null && editor.SelectedItems.Count > 0)
{
CodesetProperty newprop =
new CodesetProperty((int)editor.SelectedItems[0].Tag, editor.SelectedItems[0].Value.ToString());
return newprop;
}
}
return base.EditValue(context, provider, value);
}
private void editor_ItemDoubleClick(object sender, ItemDoubleClickEventArgs e)
{
wfes.CloseDropDown();
}
}