/// <summary> /// Compares this row with the other row provided using specified sort criteria. /// </summary> /// <param name="other">The other data row to compare this row to.</param> /// <param name="criteria">Sort criteria to use.</param> /// <returns>A 32-bit signed integer that indicates the relative order of the objects /// being compared. The return value has the following meanings: Value Meaning /// Less than zero This object is less than the other parameter. Zero This object /// is equal to other. Greater than zero This object is greater than other.</returns> public int CompareTo(DataRow other, ListSortCriteria criteria) { int res = 0; if (criteria == null || other == null || List != other.List) { return(res); } foreach (ListSortField sortFld in criteria) { DataProperty p = List[sortFld.PropertyName]; if (p != null) { object val1 = this[p.Column]; object val2 = other[p.Column]; if (val1 == val2) { res = 0; } else if (val1 == null && val2 != null) { res = -1; } else if (val1 != null && val2 == null) { res = 1; } else if (val1 is IComparable) { res = ((IComparable)val1).CompareTo(val2); } else if (val2 is IComparable) { res = -((IComparable)val2).CompareTo(val1); } else { res = string.Compare( "" + p.ResolveValue(val1, ValueFormat.DisplayString), "" + p.ResolveValue(val2, ValueFormat.DisplayString)); } if (sortFld.SortDirection == ListSortDirection.Descending) { res *= -1; } } if (res != 0) { return(res); } } return(res); }
/// <summary> /// Populates the data object list and imports the data from the given data contract list /// with ability to preserve currently selected entities. /// </summary> /// <param name="dataContract">Enumeration of data contract objects to populate the list from.</param> /// <param name="options">Additional options for the operation.</param> public override void FromDataContract(object dataContract, object options) { IEnumerable list = dataContract as IEnumerable; if (list == null) { return; } List <DataRow> sel = new List <DataRow>(); ListSortCriteria keys = new ListSortCriteria(); PopulateListOptions opts = options as PopulateListOptions; if (opts != null && opts.PreserveSelection) { sel = SelectedRows; keys.AddRange(Properties.Where(p => p.IsKey).Select(p => new ListSortField() { PropertyName = p.Name })); } data.Clear(); Reset(); SetModified(false, false); foreach (object contractItem in list) { DataRow r = new DataRow(this); data.Add(r); MoveNext(); base.FromDataContract(contractItem, options); r.Selected = sel.Any(s => SameEntity(s, r, keys)); } if (CriteriaObject != null) { AppliedCriteria = CriteriaObject.GetFieldCriteriaSettings(); } FireCollectionChanged(); FireSelectionChanged(); }
/// <summary> /// Checks if two data rows represent the same entity. Can be overridden in subclasses. /// </summary> /// <param name="r1">First row</param> /// <param name="r2">Second row</param> /// <param name="keys">Sort criteria with key property names</param> /// <returns>True, if the two rows represent the same entity, false otherwise.</returns> protected virtual bool SameEntity(DataRow r1, DataRow r2, ListSortCriteria keys) { return(r1 == null || keys == null || keys.Count == 0 ? false : r1.CompareTo(r2, keys) == 0); }