2020-11-22

业务需求:

业务工作中经常需要对表格中的数据进行处理,包括过滤、复合计算等。过滤需要有过滤条件,复合计算需要计算公式。这两种场景都需要一个表达式编辑器。GridControl自带过滤条件的表达式编辑器,我们要做的就是把这个编辑器拿出来,独立于GridControl,进而可以绑定到其它控件上。

实现原理:

找到表达式编辑器内部类UnboundColumnExpressionEditorForm,这是一个窗口类。我们将其边框设置为None,Dock属性设置为Fill,拖放到控件上,使其看上去像个控件,并公布出获取表达式字符串的方法。

效果图:

代码片段:

public class UnboundExpressionPanel : PanelControl {
      public UnboundExpressionPanel() : base() {
          BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
          StartEdit(new GridColumn()); 
      }
 
      object[] arguments;
      protected object[] Arguments { get { return arguments; } }
 
      MyUnboundColumnExpressionEditorForm form = null;
      protected MyUnboundColumnExpressionEditorForm Form { get { return form; } }
 
      private GridColumn fUnboundColumn;
      public GridColumn UnboundColumn {
          get { return fUnboundColumn; }
          set {
              if (fUnboundColumn == value) return;
              StartEdit(value);
          }
      }
 
      protected MyUnboundColumnExpressionEditorForm CreateForm(params object[] arguments)
      {
          return new MyUnboundColumnExpressionEditorForm(arguments[0], null);
      }
 
      protected void ApplyExpression(string expression) {
          if (Arguments == null) return;
          ((GridColumn)Arguments[0]).UnboundExpression = expression;
      }
 
      public void StartEdit(params object[] arguments) {
          if (arguments.Length < 1) return;
          GridColumn unboundColumn = arguments[0] as GridColumn;
          if (unboundColumn == null) return;
          fUnboundColumn = unboundColumn;
          DestroyForm();
          this.arguments = arguments;
          this.form = CreateForm(arguments);
          if (form == null) return;
          form.Dock = DockStyle.Fill;
          form.TopLevel = false;
          form.FormBorderStyle = FormBorderStyle.None;
          form.Closing += new CancelEventHandler(form_Closing);
          form.buttonCancel.Click += new EventHandler(buttonCancel_Click);
          form.buttonOK.Text = "Apply";
          Controls.Add(form);
          form.Visible = true;
      }
 
      void buttonCancel_Click(object sender, EventArgs e)
      {
          if (form != null) form.Close();
      }
 
      void form_Closing(object sender, CancelEventArgs e)
      {
          e.Cancel = true;
          if (this.arguments == null || this.form == null) return;
          if (form.DialogResult == DialogResult.OK)
          {
              ApplyExpression(form.Expression);
          }
          else
          {
              form.ResetMemoText();
          }
      }
 
      public void DestroyForm()
      {
          if (form != null) form.Dispose();
          form = null;
      }
  }
 
  public class MyUnboundColumnExpressionEditorForm : UnboundColumnExpressionEditorForm
  {
      public MyUnboundColumnExpressionEditorForm(object contextInstance, 
          IDesignerHost designerHost) 
          : base(contextInstance, designerHost) { }
 
      private string GetExpressionMemoEditText()
      {
          GridColumn column = ContextInstance as GridColumn;
          return null == column ? string.Empty : column.UnboundExpression;
      }
 
      public void ResetMemoText()
      {
          ExpressionMemoEdit.Text = GetExpressionMemoEditText();
      }
  }

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Java 中的 LocalDateTime 类来处理时间。 首先,我们需要将给定的时间段转换为 LocalDateTime 对象: ```java LocalDateTime start = LocalDateTime.of(2020, 11, 20, 0, 0, 0); LocalDateTime end = LocalDateTime.of(2021, 10, 9, 23, 59, 59); ``` 这里将结束时间设置为 23:59:59 是因为我们想要包含该日期的所有时间。 接下来,我们可以使用一个循环来遍历时间段中的每一天,并获取该天的开始时间和结束时间: ```java LocalDateTime current = start; while (!current.isAfter(end)) { LocalDateTime dayStart = current.withHour(0).withMinute(0).withSecond(0).withNano(0); LocalDateTime dayEnd = current.withHour(23).withMinute(59).withSecond(59).withNano(999999999); System.out.println("Day " + current.toLocalDate() + " starts at " + dayStart + " and ends at " + dayEnd); current = current.plusDays(1); } ``` 这里,我们使用 `withHour`、`withMinute`、`withSecond` 和 `withNano` 方法来设置每天的开始时间和结束时间。然后,我们可以使用 `plusDays` 方法来增加 `current` 对象的日期,以便遍历整个时间段。 完整代码如下: ```java import java.time.LocalDateTime; public class Main { public static void main(String[] args) { LocalDateTime start = LocalDateTime.of(2020, 11, 20, 0, 0, 0); LocalDateTime end = LocalDateTime.of(2021, 10, 9, 23, 59, 59); LocalDateTime current = start; while (!current.isAfter(end)) { LocalDateTime dayStart = current.withHour(0).withMinute(0).withSecond(0).withNano(0); LocalDateTime dayEnd = current.withHour(23).withMinute(59).withSecond(59).withNano(999999999); System.out.println("Day " + current.toLocalDate() + " starts at " + dayStart + " and ends at " + dayEnd); current = current.plusDays(1); } } } ``` 输出结果如下: ``` Day 2020-11-20 starts at 2020-11-20T00:00 and ends at 2020-11-20T23:59:59.999999999 Day 2020-11-21 starts at 2020-11-21T00:00 and ends at 2020-11-21T23:59:59.999999999 Day 2020-11-22 starts at 2020-11-22T00:00 and ends at 2020-11-22T23:59:59.999999999 ... Day 2021-10-07 starts at 2021-10-07T00:00 and ends at 2021-10-07T23:59:59.999999999 Day 2021-10-08 starts at 2021-10-08T00:00 and ends at 2021-10-08T23:59:59.999999999 Day 2021-10-09 starts at 2021-10-09T00:00 and ends at 2021-10-09T23:59:59.999999999 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值