DataGridView使用Business Objects进行列排序 [英] DataGridView Column sorting with Business Objects
问题描述
我正在设置这样的DataGridView:
I am setting-up my DataGridView like this:
jobs = new List<DisplayJob>();
uxJobList.AutoGenerateColumns = false;
jobListBindingSource.DataSource = jobs;
uxJobList.DataSource = jobListBindingSource;
int newColumn;
newColumn = uxJobList.Columns.Add("Id", "Job No.");
uxJobList.Columns[newColumn].DataPropertyName = "Id";
uxJobList.Columns[newColumn].DefaultCellStyle.Format = Global.JobIdFormat;
uxJobList.Columns[newColumn].DefaultCellStyle.Font = new Font(uxJobList.DefaultCellStyle.Font, FontStyle.Bold);
uxJobList.Columns[newColumn].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
uxJobList.Columns[newColumn].Width = 62;
uxJobList.Columns[newColumn].Resizable = DataGridViewTriState.False;
uxJobList.Columns[newColumn].SortMode = DataGridViewColumnSortMode.Automatic;
:
:
DisplayJob类的外观如下:
where the DisplayJob class looks like:
public class DisplayJob
{
public DisplayJob(int id)
{
Id = id;
}
public DisplayJob(JobEntity job)
{
Id = job.Id;
Type = job.JobTypeDescription;
CreatedAt = job.CreatedAt;
StartedAt = job.StartedAt;
ExternalStatus = job.ExternalStatus;
FriendlyExternalStatus = job.FriendlyExternalStatus;
ExternalStatusFriendly = job.ExternalStatusFriendly;
CustomerName = job.Customer.Name;
CustomerKey = job.Customer.CustomerKey;
WorkAddress = job.WorkAddress;
CreatedBy = job.CreatedBy;
CancelledAt = job.CancelledAt;
ClosedAt = job.ClosedAt;
ReasonWaiting = job.ReasonWaiting;
CancelledBy = job.CancelledBy;
CancelledReason = job.CancelledReason;
DisplayCreator = Global.GetDisplayName(CreatedBy);
ActionRedoNeeded = job.ActionRedoNeeded;
if (job.Scheme != null)
{
SchemeCode = job.Scheme.Code;
}
}
public int Id { get; private set; }
public string Type { get; private set; }
public DateTime CreatedAt { get; private set; }
public DateTime? StartedAt { get; private set; }
public string ExternalStatus { get; private set; }
public string FriendlyExternalStatus { get; private set; }
public string ExternalStatusFriendly { get; private set; }
public string CustomerName { get; private set; }
public string CustomerKey { get; private set; }
public string WorkAddress { get; private set; }
public string CreatedBy { get; private set; }
public DateTime? CancelledAt { get; private set; }
public DateTime? ClosedAt { get; private set; }
public string CancelledBy { get; private set; }
public string ReasonWaiting { get; private set; }
public string DisplayCreator { get; private set; }
public string CancelledReason { get; private set; }
public string SchemeCode { get; private set; }
public bool ActionRedoNeeded { get; private set; }
}
但列排序不起作用。
推荐答案
如果您希望支持对集合进行排序和搜索,所有它需要它从您的BindingList参数化类型派生一个类,并覆盖一些基类方法和属性。
If you want to support sorting and searching on the collection, all it takes it to derive a class from your BindingList parameterized type, and override a few base class methods and properties.
最好的方法是扩展BindingList并执行以下操作:
The best way is to extend the BindingList and do those following things:
protected override bool SupportsSearchingCore
{
get
{
return true;
}
}
protected override bool SupportsSortingCore
{
get { return true; }
}
您还需要实现排序代码:
You will also need to implement the sort code:
ListSortDirection sortDirectionValue;
PropertyDescriptor sortPropertyValue;
protected override void ApplySortCore(PropertyDescriptor prop,
ListSortDirection direction)
{
sortedList = new ArrayList();
// Check to see if the property type we are sorting by implements
// the IComparable interface.
Type interfaceType = prop.PropertyType.GetInterface("IComparable");
if (interfaceType != null)
{
// If so, set the SortPropertyValue and SortDirectionValue.
sortPropertyValue = prop;
sortDirectionValue = direction;
unsortedItems = new ArrayList(this.Count);
// Loop through each item, adding it the the sortedItems ArrayList.
foreach (Object item in this.Items) {
sortedList.Add(prop.GetValue(item));
unsortedItems.Add(item);
}
// Call Sort on the ArrayList.
sortedList.Sort();
T temp;
// Check the sort direction and then copy the sorted items
// back into the list.
if (direction == ListSortDirection.Descending)
sortedList.Reverse();
for (int i = 0; i < this.Count; i++)
{
int position = Find(prop.Name, sortedList[i]);
if (position != i) {
temp = this[i];
this[i] = this[position];
this[position] = temp;
}
}
isSortedValue = true;
// Raise the ListChanged event so bound controls refresh their
// values.
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
else
// If the property type does not implement IComparable, let the user
// know.
throw new NotSupportedException("Cannot sort by " + prop.Name +
". This" + prop.PropertyType.ToString() +
" does not implement IComparable");
}
如果你需要更多的信息,你可以随时去那里, a href =http://msdn.microsoft.com/en-us/library/aa480736.aspx =nofollow noreferrer>如何扩展绑定列表。
If you need more information you can always go there and get all explication about how to extend the binding list.
这篇关于DataGridView使用Business Objects进行列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!