1.ListView 添加日期筛选下拉选择,选择指定,可指定日期范围
2.Code
using DevExpress.Data.Filtering; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Actions; using DevExpress.ExpressApp.DC; using DevExpress.ExpressApp.Editors; using DevExpress.ExpressApp.SystemModule; using DevExpress.ExpressApp.Templates; using DevExpress.Persistent.Base; using DevExpress.Persistent.Validation; using DevExpress.Xpo; using DevExpress.XtraPrinting.Native; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; namespace E1554.Module { public interface ISupportFilterByDate { // Properties DateTime Date { get; } } [NonPersistent] public class DateInterval { private DateTime _start; private DateTime _end; public DateTime Start { get { return this._start; } set { this._start = value; bool flag = value > this._end; if (flag) { this._end = this._start; } } } public DateTime End { get { return this._end; } set { this._end = value; bool flag = value < this._start; if (flag) { this._start = value; } } } } public enum TransactionStatus { [XafDisplayName("Cancelled")] Cancelled = 3, [XafDisplayName("Comlited")] Comlited = 2, [XafDisplayName("Save")] Save = 1 } public class CreateDataFilterController : ViewController { // Fields private IContainer components = null; private DetailView detailView; private IObjectSpace os; private SingleChoiceAction saFilterByDate; public static int SelectedIndex; // Methods public CreateDataFilterController() { this.InitializeComponent(); base.RegisterActions(this.components); this.saFilterByDate = new SingleChoiceAction(this, "FilterByDateID", PredefinedCategory.Search); this.saFilterByDate.Category = "Filters"; this.saFilterByDate.TargetObjectType = typeof(ISupportFilterByDate); ChoiceActionItem item = new ChoiceActionItem("所有", null) { Id = "All" }; this.saFilterByDate.Items.Add(item); item = new ChoiceActionItem("今天", null) { Id = "Today" }; this.saFilterByDate.Items.Add(item); item = new ChoiceActionItem("本周", null) { Id = "ThisWeek" }; this.saFilterByDate.Items.Add(item); item = new ChoiceActionItem("上周", null) { Id = "PrevWeek" }; this.saFilterByDate.Items.Add(item); item = new ChoiceActionItem("兩周", null) { Id = "TwoWeek" }; this.saFilterByDate.Items.Add(item); item = new ChoiceActionItem("本月", null) { Id = "ThisMonth" }; this.saFilterByDate.Items.Add(item); item = new ChoiceActionItem("上月", null) { Id = "PrevMonth" }; this.saFilterByDate.Items.Add(item); //今天,本週,上週,兩週,本月,上月,今年,去年,指定 item = new ChoiceActionItem("今年", null) { Id = "ThisYear" }; this.saFilterByDate.Items.Add(item); item = new ChoiceActionItem("去年", null) { Id = "PrevYear" }; this.saFilterByDate.Items.Add(item); item = new ChoiceActionItem("指定", null) { Id = "DatesInterval" }; this.saFilterByDate.Items.Add(item); this.saFilterByDate.Caption = "按日期"; this.saFilterByDate.PaintStyle = ActionItemPaintStyle.Caption; this.saFilterByDate.Execute += new SingleChoiceActionExecuteEventHandler(this.saFilterByDate_Execute); base.ViewControlsCreated += new EventHandler(this.CreateDataFilterController_ViewControlsCreated); } private void CreateDataFilterController_Activated(object sender, EventArgs e) { this.SetCurrentFilterItem(); } private void CreateDataFilterController_ViewControlsCreated(object sender, EventArgs e) { this.saFilterByDate.Active.SetItemValue("HIDE_FOR_MONTH_STATISTIC", true); //this.saFilterByDate.Active.SetItemValue("HIDE_FOR_MONTH_STATISTIC", (base.View.Id != "SaleItem_PivotView_ByMonth_Chart_Common") && !(base.View.Id == "SaleItem_Share_ByMonth_Chart_Common")); } private void dc_Accepting(object sender, DialogControllerAcceptingEventArgs e) { DateInterval currentObject = e.AcceptActionArgs.CurrentObject as DateInterval; ListView view = base.View as ListView; CriteriaOperator @operator = Helper.GetFilterByDates(view.ObjectTypeInfo.Type, currentObject.Start, currentObject.End); if (!view.CollectionSource.Criteria.ContainsKey("DateFilter")) { view.CollectionSource.Criteria.Add(new KeyValuePair<string, CriteriaOperator>("DateFilter", @operator)); } else { view.CollectionSource.Criteria["DateFilter"] = @operator; } } protected override void Dispose(bool disposing) { if (disposing && (this.components != null)) { this.components.Dispose(); } base.Dispose(disposing); } private void Filter(ChoiceActionItem item, SingleChoiceActionExecuteEventArgs e) { bool flag = base.View.Model.GetValue<bool>("UseServerMode"); ListView view = base.View as ListView; if ((view != null) && (item != null)) { if (item.Id == "DatesInterval") { #region MyRegion this.os = base.ObjectSpace.CreateNestedObjectSpace(); DateInterval interval1 = new DateInterval { Start = DateTime.Now.Date, End = DateTime.Now }; this.detailView = base.Application.CreateDetailView(this.os, interval1); this.detailView.ViewEditMode = ViewEditMode.View; this.os.Committed += new EventHandler(this.os_Committed); e.ShowViewParameters.CreatedView = this.detailView; //if (base.Application.GetType().Name == "WinterQueenAspNetApplication") //{ // e.ShowViewParameters.TargetWindow = TargetWindow.NewModalWindow; //} e.ShowViewParameters.Context = TemplateContext.PopupWindow; DialogController controller = base.Application.CreateController<DialogController>(); controller.Accepting += new EventHandler<DialogControllerAcceptingEventArgs>(this.dc_Accepting); e.ShowViewParameters.Controllers.Add(controller); #endregion } else { DateTime minValue = DateTime.MinValue; CriteriaOperator left = null; if (item.Id == "All") { view.CollectionSource.Criteria[0] = null; } else { #region MyRegion string dateFieldStr = "Date";// "CreateOn"; #region MyRegion if (item.Id == "Today") { if (view.Model.ModelClass.ShortName == "SaleItem") { left = CriteriaOperator.Parse("[Sale_ID.DateConfirm] >= LocalDateTimeToday()", new object[0]); } else { left = CriteriaOperator.Parse(string.Format("[{0}] >= LocalDateTimeToday()", dateFieldStr), new object[0]); } } if (item.Id == "ThisWeek") { if (view.Model.ModelClass.ShortName == "SaleItem") { left = CriteriaOperator.Parse("[Sale_ID.DateConfirm] >= LocalDateTimeThisWeek()", new object[0]); } else { left = CriteriaOperator.Parse(string.Format("IsThisWeek([{0}])", dateFieldStr), new object[0]); } } if (item.Id == "PrevWeek") { if (view.Model.ModelClass.ShortName == "SaleItem") { left = CriteriaOperator.Parse("[Sale_ID.DateConfirm] >= LocalDateTimeThisWeek()", new object[0]); } else// IsOutlookIntervalLastWeek { left = CriteriaOperator.Parse(string.Format("IsOutlookIntervalLastWeek([{0}])", dateFieldStr), new object[0]); } } if (item.Id == "TwoWeek") { if (view.Model.ModelClass.ShortName == "SaleItem") { left = CriteriaOperator.Parse("[Sale_ID.DateConfirm] >= LocalDateTimeThisWeek()", new object[0]); } else { left = CriteriaOperator.Parse(string.Format("[{0}] > ADDDAYS(LocalDateTimeToday(), -14) AND [{0}] <LocalDateTimeToday()", dateFieldStr), new object[0]); } } #endregion #region MyRegion if (item.Id == "ThisMonth") { if (view.Model.ModelClass.ShortName == "SaleItem") { left = CriteriaOperator.Parse("[Sale_ID.DateConfirm]>= LocalDateTimeThisMonth()", new object[0]); } else { left = CriteriaOperator.Parse(string.Format("IsThisMonth([{0}])", dateFieldStr), new object[0]); } } if (item.Id == "PrevMonth") { if (view.Model.ModelClass.ShortName == "SaleItem") { left = CriteriaOperator.Parse("[Sale_ID.DateConfirm]>= LocalDateTimeThisMonth()", new object[0]); } else { left = CriteriaOperator.Parse(string.Format("IsThisMonth(AddMonths([{0}],1))", dateFieldStr), new object[0]); } } if (item.Id == "ThisYear") { if (view.Model.ModelClass.ShortName == "SaleItem") { left = CriteriaOperator.Parse("[Sale_ID.DateConfirm]>=LocalDateTimeThisYear()", new object[0]); } else { left = CriteriaOperator.Parse(string.Format("IsThisYear([{0}])", dateFieldStr), new object[0]); } } if (item.Id == "PrevYear") { if (view.Model.ModelClass.ShortName == "SaleItem") { left = CriteriaOperator.Parse("[Sale_ID.DateConfirm]>=LocalDateTimeThisYear()", new object[0]); } else { left = CriteriaOperator.Parse(string.Format("IsThisYear(AddYears([{0}],1))", dateFieldStr), new object[0]); } } //if (item.Id == "PrevMonth") //{ // DateTime time3 = DateTime.Now.AddMonths(-1); // minValue = new DateTime(time3.Year, time3.Month, 1); // DateTime time4 = minValue.AddMonths(1); // if (view.Model.ModelClass.ShortName == "SaleItem") // { // object[] parameters = new object[] { minValue, time4 }; // left = CriteriaOperator.Parse("[Sale_ID.DateConfirm]>=? AND [Sale_ID.DateConfirm]<=?", parameters); // } // else // { // object[] objArray2 = new object[] { minValue, time4 }; // left = CriteriaOperator.Parse("Date>=? AND Date<=?", objArray2); // } //} #endregion if (view.Model.ModelClass.ShortName == "Sale") { object[] objArray3 = new object[] { TransactionStatus.Comlited }; left = CriteriaOperator.And(left, CriteriaOperator.Parse("TransactionStatus=?", objArray3)); } if (view.Model.ModelClass.ShortName == "SaleItem") { object[] objArray4 = new object[] { TransactionStatus.Comlited }; left = CriteriaOperator.And(left, CriteriaOperator.Parse("Sale_ID.TransactionStatus=?", objArray4)); } if (!view.CollectionSource.Criteria.ContainsKey("DateFilter")) { view.CollectionSource.Criteria.Add(new KeyValuePair<string, CriteriaOperator>("DateFilter", left)); } else { view.CollectionSource.Criteria["DateFilter"] = left; } #endregion } } } } private void InitializeComponent() { base.TargetObjectType = typeof(ISupportFilterByDate); base.TargetViewNesting = Nesting.Root; base.TargetViewType = ViewType.ListView; base.TypeOfView = typeof(ListView); base.Activated += new EventHandler(this.CreateDataFilterController_Activated); } private void os_Committed(object sender, EventArgs e) { } private void saFilterByDate_Execute(object sender, SingleChoiceActionExecuteEventArgs e) { this.Filter(this.saFilterByDate.SelectedItem, e); } private void SetCurrentFilterItem() { this.saFilterByDate.SelectedIndex = 2; this.Filter(this.saFilterByDate.Items[2], null); } // Properties public SingleChoiceAction FilterByDate { get { return this.saFilterByDate; } } } public class Helper { // Methods public static CriteriaOperator GetFilterByDates(Type obj, DateTime start, DateTime end) { CriteriaOperator @operator = null; if (obj.GetInterface("ISupportFilterByDate") != null) { object[] parameters = new object[] { start, end }; MethodInfo method = obj.GetMethod("GetFilterString"); if (method != null) { @operator = method.Invoke(null, parameters) as CriteriaOperator; } } return @operator; } } }
using System; using DevExpress.Xpo; using DevExpress.ExpressApp; using DevExpress.Persistent.Base; using DevExpress.Persistent.BaseImpl; using DevExpress.Persistent.Validation; using DevExpress.Data.Filtering; using DevExpress.ExpressApp.SystemModule; namespace E1554.Module { [DefaultClassOptions] public class Detail : BaseObject, ISupportFilterByDate { public Detail(Session session) : base(session) { } private string _DetailName; public string DetailName { get { return _DetailName; } set { SetPropertyValue("DetailName", ref _DetailName, value); } } private Master _Master; [Association("Master-Details")] public Master Master { get { return _Master; } set { SetPropertyValue("Master", ref _Master, value); } } private int _Qty; public int Qty { get { return _Qty;} set { int oldQty = _Qty; SetPropertyValue("Qty", ref _Qty, value); if (!IsLoading && !IsSaving && oldQty != _Qty) { mDefaultAddress = Qty.ToString(); OnChanged("DefaultAddress"); } } } [Persistent("DefaultAddress")] private string mDefaultAddress=null; [PersistentAlias("mDefaultAddress")] public string DefaultAddress { get { if (!IsLoading && !IsSaving && mDefaultAddress == null) mDefaultAddress = Qty.ToString(); return mDefaultAddress; } set { SetPropertyValue("DefaultAddress", ref mDefaultAddress, value); } } private DateTime date; public DateTime Date { get { return date; } set { SetPropertyValue("Date", ref date, value); } } public static CriteriaOperator GetFilterString(DateTime start, DateTime end) { return CriteriaOperator.Parse("Date>=? AND Date<=?", new object[] { start, end }); } } }
欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/