EDN Admin
Well-known member
I am basically trying to generate a DropDownStyle list in a DataGridViewTextBoxColumn and although I can get the property to show (using the MSDN default) for some reason I cant seem to change the value from the default as seen below. What basically happens
is the DropDownList gets added to the PropertyGrid of the DataGridView but the property itself doesnt keep the value selected. What am I missing?
public class StandardValuesIntConverter : System.ComponentModel.TypeConverter<br/>
{<br/>
private ArrayList values;<br/>
public StandardValuesIntConverter()<br/>
{<br/>
// Initializes the standard values list with defaults.<br/>
values = new ArrayList(new int[] { 1, 2, 3, 4, 5 });<br/>
}<br/>
// Indicates this converter provides a list of standard values.<br/>
public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)<br/>
{<br/>
return true;<br/>
}<br/>
// Returns a StandardValuesCollection of standard value objects.<br/>
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(System.ComponentModel.ITypeDescriptorContext context)<br/>
{
<br/>
// Passes the local integer array.<br/>
StandardValuesCollection svc = new StandardValuesCollection(values);
<br/>
return svc;<br/>
}<br/>
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)<br/>
{<br/>
if( sourceType == typeof(string) )<br/>
return true;<br/>
return base.CanConvertFrom(context, sourceType);<br/>
}<br/>
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)<br/>
{<br/>
if( value.GetType() == typeof(string) )<br/>
{<br/>
// Parses the string to get the integer to set to the property.<br/>
int newVal = int.Parse((string)value);<br/>
<br/>
// Tests whether new integer is already in the list.<br/>
if( !values.Contains(newVal) )<br/>
{<br/>
// If the integer is not in list, adds it in order.<br/>
values.Add(newVal);<br/>
values.Sort();<br/>
}
<br/>
// Returns the integer value to assign to the property.<br/>
return newVal;<br/>
}<br/>
return base.ConvertFrom(context, culture, value);<br/>
}<br/>
}<br/>
// Provides a test control with an integer property associated with
<br/>
// the StandardValuesIntConverter type converter.<br/>
public class NewDataGridViewTextBoxColumn : System.Windows.Forms.DataGridViewTextBoxColumn<br/>
{<br/>
[Category("Behaviour"), NotifyParentProperty(true)]<br/>
[TypeConverter(typeof(StandardValuesIntConverter))]<br/>
public int TestInt<br/>
{<br/>
get<br/>
{<br/>
return this.integer_field;<br/>
}<br/>
set<br/>
{<br/>
if(value.GetType() == typeof(int))<br/>
this.integer_field = value;<br/>
}<br/>
}<br/>
private int integer_field = 0;<br/>
public NewDataGridViewTextBoxColumn( )<br/>
{<br/>
}<br/>
}
<br/>
View the full article
is the DropDownList gets added to the PropertyGrid of the DataGridView but the property itself doesnt keep the value selected. What am I missing?
public class StandardValuesIntConverter : System.ComponentModel.TypeConverter<br/>
{<br/>
private ArrayList values;<br/>
public StandardValuesIntConverter()<br/>
{<br/>
// Initializes the standard values list with defaults.<br/>
values = new ArrayList(new int[] { 1, 2, 3, 4, 5 });<br/>
}<br/>
// Indicates this converter provides a list of standard values.<br/>
public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)<br/>
{<br/>
return true;<br/>
}<br/>
// Returns a StandardValuesCollection of standard value objects.<br/>
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(System.ComponentModel.ITypeDescriptorContext context)<br/>
{
<br/>
// Passes the local integer array.<br/>
StandardValuesCollection svc = new StandardValuesCollection(values);
<br/>
return svc;<br/>
}<br/>
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)<br/>
{<br/>
if( sourceType == typeof(string) )<br/>
return true;<br/>
return base.CanConvertFrom(context, sourceType);<br/>
}<br/>
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)<br/>
{<br/>
if( value.GetType() == typeof(string) )<br/>
{<br/>
// Parses the string to get the integer to set to the property.<br/>
int newVal = int.Parse((string)value);<br/>
<br/>
// Tests whether new integer is already in the list.<br/>
if( !values.Contains(newVal) )<br/>
{<br/>
// If the integer is not in list, adds it in order.<br/>
values.Add(newVal);<br/>
values.Sort();<br/>
}
<br/>
// Returns the integer value to assign to the property.<br/>
return newVal;<br/>
}<br/>
return base.ConvertFrom(context, culture, value);<br/>
}<br/>
}<br/>
// Provides a test control with an integer property associated with
<br/>
// the StandardValuesIntConverter type converter.<br/>
public class NewDataGridViewTextBoxColumn : System.Windows.Forms.DataGridViewTextBoxColumn<br/>
{<br/>
[Category("Behaviour"), NotifyParentProperty(true)]<br/>
[TypeConverter(typeof(StandardValuesIntConverter))]<br/>
public int TestInt<br/>
{<br/>
get<br/>
{<br/>
return this.integer_field;<br/>
}<br/>
set<br/>
{<br/>
if(value.GetType() == typeof(int))<br/>
this.integer_field = value;<br/>
}<br/>
}<br/>
private int integer_field = 0;<br/>
public NewDataGridViewTextBoxColumn( )<br/>
{<br/>
}<br/>
}
<br/>
View the full article