I have surfed the net for two days but I haven't yet found an answer.
I have a BindingList<T> derived class.
I can bind it to a datagridview without any problem (after two weeks!!!)
My "T" class has a group of public method and I have seen that the column name related to each single method is exactly the name of the method.
In some cases it is not nice at least because the name of the method cannot be with spaces
I have tried with attribute but I have found nothing about Column name
After a lot of surf I have reached this point.
I have seen that the GetItemProperties function filters the members of the class "T" according to the attribute property Browsable
the attribute class is this
the comparer class so that I can easily exchange the order of the column
and then the "T" class
These attributes changes correctly the order according to the number and make a column disappear according to the true/false value.
What I would like to know is: there is the possibility to put the string in the column name?
... forgot the binding ...
Any suggestion?
Of course I don't want to create the column by hand!
Thanks a lot
Davide
I have a BindingList<T> derived class.
I can bind it to a datagridview without any problem (after two weeks!!!)
My "T" class has a group of public method and I have seen that the column name related to each single method is exactly the name of the method.
In some cases it is not nice at least because the name of the method cannot be with spaces
I have tried with attribute but I have found nothing about Column name
After a lot of surf I have reached this point.
Code:
public class MyBindingList<T> : BindingList<T>, ITypedList
{
#region ITypedList Membri di
public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
{
Type myType = typeof(T);
PropertyDescriptorCollection typePropertiesCollection = TypeDescriptor.GetProperties(myType);
ArrayList propertyDescriptorsToUse = new ArrayList();
foreach (PropertyDescriptor property in typePropertiesCollection)
{
PropDescriptionAttribute myAttr = (PropDescriptionAttribute)property.Attributes[typeof(PropDescriptionAttribute)];
if (myAttr != null)
{
if (!myAttr.Browsable)
continue;
}
propertyDescriptorsToUse.Add(property);
}
PropDescriptionAttributeComparer pc = new PropDescriptionAttributeComparer();
propertyDescriptorsToUse.Sort(pc);
return new PropertyDescriptorCollection((PropertyDescriptor[])propertyDescriptorsToUse.ToArray(typeof(PropertyDescriptor)));
}
public string GetListName(PropertyDescriptor[] listAccessors)
{
return "???"; // I don't know what is this... it is never called
}
#endregion
}
I have seen that the GetItemProperties function filters the members of the class "T" according to the attribute property Browsable
the attribute class is this
Code:
[AttributeUsage(AttributeTargets.Property)]
public class PropDescriptionAttribute : Attribute
{
#region Member declaration
/// <summary>Browsable or not</summary>
private bool browsable;
/// <summary>Browsable or not</summary>
public bool Browsable
{
get
{
return browsable;
}
}
/// <summary>Name displayed</summary>
private string name;
/// <summary>Name displayed</summary>
public string Name
{
get { return name; }
}
/// <summary>Order</summary>
private int index;
/// <summary>Order</summary>
public int Index
{
get
{
return index;
}
}
#endregion
/// <summary>Constructor</summary>
/// <param name="b">browsable</param>
/// <param name="s">name of the column</param>
/// <param name="i">position</param>
public PropDescriptionAttribute(bool b, string s, int i)
{
browsable = b;
name = s;
index = i;
}
}
Code:
class PropDescriptionAttributeComparer : IComparer
{
#region IComparer Membri di
public int Compare(object x, object y)
{
PropertyDescriptor pdx = x as PropertyDescriptor;
PropertyDescriptor pdy = y as PropertyDescriptor;
if (pdx == null)
return -1;
if (pdx == null)
return 1;
PropDescriptionAttribute propX = (PropDescriptionAttribute)pdx.Attributes[typeof(PropDescriptionAttribute)];
if (propX == null)
return -1;
PropDescriptionAttribute propY = (PropDescriptionAttribute)pdy.Attributes[typeof(PropDescriptionAttribute)];
if (propY == null)
return 1;
return propX.Index.CompareTo(propY.Index);
}
#endregion
}
and then the "T" class
Code:
public class Person : INotifyPropertyChanged
{
private string name = "";
[PropDescriptionAttribute(false, "Nome", 1)]
public string Name
{
get { return name; }
private set
{
if (name != value)
{
name = value;
NotifyPropertyChanged("Name");
}
}
}
private int age = 0;
[PropDescription(true, "Età", 2)]
public int Age
{
get { return age; }
set
{
if (age != value)
{
age = value;
NotifyPropertyChanged("Age");
}
}
}
public Person(string n, int a)
{
Name = n;
Age = a;
}
public string WhoAmI()
{
return "I am " + Name;
}
}
What I would like to know is: there is the possibility to put the string in the column name?
... forgot the binding ...
Code:
private MyBindingList<Person> group = new MyBindingList<Person>();
private BindingSource bs = new BindingSource();
public void Bind()
{
bs.DataSource = group;
dgvBinding.AutoGenerateColumn = true;
dgvBinding.DataSource = bs.DataSource;
}
Any suggestion?
Of course I don't want to create the column by hand!
Thanks a lot
Davide