Hi,
rob said:
Great, it works now. There are two problems left, though:
1) The glyph is not shown anymore on any of the colums when I set the
datasource property of my datagridview to the bindingsource. This works
fine when I create rows on my datagridview and manually set each cell's
content.
If you mean the sorting glyph, then that's because the list the
BindingSource is using doesn't support sorting. In case you didn't assign a
list to the BindingSource.DataSource then the default list is a
BindingList<T> which doesn't support sorting.
You could use a SortableBindingList<T> posted at the end of this message.
Create a Data Source (Menu-Data) for your class (eg. Customer), then from
the Data Sources window drag it on the Form. Then from code in form_load
assign the actual list to be used, example:
private void Form_Load(...)
{
someBindingSource.DataSource = new SortableBindingList<Customer>();
someBindingSource.Add( new Customer("fkdsf") );
someBindingSource.Add( new Customer("fdsf") );
}
But sorting the 'Date' column correctly while being a string would be hard
and require more code, so if it is possible use a DateTime for your Date
column.
2) This approach is much slower (factor 1.5) then by setting the
content of each cell manually. Is there a way to improve this?
Not sure about this, probely some databinding overhead, gonna have a look
later, but i doubt i'll find something.
HTH,
Greetings
The code for SortableBindingList<T> is at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms02182005.asp ,
but because some parts are missing, i pasted the complete code here:
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace test1.CellFormat
{
public class SortableBindingList<T> : BindingList<T>
{
private PropertyDescriptor sortProp = null;
private ListSortDirection sortDir =
ListSortDirection.Ascending;
private bool isSorted = false;
protected override bool SupportsSortingCore
{
get { return true; }
}
protected override void ApplySortCore(PropertyDescriptor property,
ListSortDirection direction)
{
PropertyComparer<T> pc =
new PropertyComparer<T>(property, direction);
((List<T>)this.Items).Sort(pc);
isSorted = true;
sortProp = property;
sortDir = direction;
// Let bound controls know they should refresh their views
this.OnListChanged(new
ListChangedEventArgs(ListChangedType.Reset, -1));
}
protected override bool IsSortedCore
{
get { return isSorted; }
}
protected override void RemoveSortCore()
{
// can't unsort
}
protected override PropertyDescriptor SortPropertyCore
{
get { return sortProp; }
}
protected override ListSortDirection SortDirectionCore
{
get { return sortDir; }
}
}
public class PropertyComparer<T> : System.Collections.Generic.IComparer<T>
{
// The following code contains code implemented by Rockford Lhotka:
//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet01272004.asp
private PropertyDescriptor _property;
private ListSortDirection _direction;
public PropertyComparer(PropertyDescriptor property, ListSortDirection
direction)
{
_property = property;
_direction = direction;
}
#region IComparer<T>
public int Compare(T xWord, T yWord)
{
// Get property values
object xValue = GetPropertyValue(xWord, _property.Name);
object yValue = GetPropertyValue(yWord, _property.Name);
// Determine sort order
if (_direction == ListSortDirection.Ascending)
{
return CompareAscending(xValue, yValue);
}
else
{
return CompareDescending(xValue, yValue);
}
}
public bool Equals(T xWord, T yWord)
{
return xWord.Equals(yWord);
}
public int GetHashCode(T obj)
{
return obj.GetHashCode();
}
#endregion
// Compare two property values of any type
private int CompareAscending(object xValue, object yValue)
{
int result;
// If values implement IComparer
if (xValue is IComparable)
{
result = ((IComparable)xValue).CompareTo(yValue);
}
// If values don't implement IComparer but are equivalent
else if (xValue.Equals(yValue))
{
result = 0;
}
// Values don't implement IComparer and are not equivalent, so compare
as string values
else result = xValue.ToString().CompareTo(yValue.ToString());
// Return result
return result;
}
private int CompareDescending(object xValue, object yValue)
{
// Return result adjusted for ascending or descending sort order ie
// multiplied by 1 for ascending or -1 for descending
return CompareAscending(xValue, yValue) * -1;
}
private object GetPropertyValue(T value, string property)
{
// Get property
PropertyInfo propertyInfo = value.GetType().GetProperty(property);
// Return value
return propertyInfo.GetValue(value, null);
}
}
}