【发布时间】:2014-03-07 23:31:00
【问题描述】:
我有一个 GridView,它包含三列名称、开始日期和结束日期。 时间范围按排序顺序排列。表示 GridView 默认按开始日期排序。 用户想要插入一个带有名称、开始日期和结束日期的新行。 如果新开始日期和新结束日期之间的时间跨度与其他行的任何其他(可能超过一行)时间跨度相交。这些行将被删除,并且此新记录将插入到 GridView 中。如果与任何时间跨度都没有交集,则只需将其添加到 GridView。
如果编写了以下代码来找出两个日期之间的交集(数据类型为日期时间)。
public class DateTimeRange
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public bool Intersects(DateTimeRange test)
{
if (this.Start > this.End || test.Start > test.End)
return false;
//throw new InvalidDateRangeException();
if (this.Start == this.End || test.Start == test.End)
return false; // No actual date range
if (this.Start == test.Start || this.End == test.End)
return true; // If any set is the same time, then by default there must be some overlap.
if (this.Start < test.Start)
{
if (this.End > test.Start && this.End < test.End)
return true; // Condition 1
if (this.End > test.End)
return true; // Condition 3
}
else
{
if (test.End > this.Start && test.End < this.End)
return true; // Condition 2
if (test.End > this.End)
return true; // Condition 4
}
return false;
}
}
所以我打算逐行迭代 GridView,存储与新重新编码相交的行的索引。在迭代之后,我将删除这些索引的行并插入新的重新编码。 所以我怀疑有没有更好的方法可以更简单或更简单地做到这一点。
前提条件:所有时间范围最初都是互斥的,表示行之间没有交集。
【问题讨论】:
-
您的问题是什么?如果您询问您的代码,我不确定它是否会起作用,我相信您可以用更少的 if-else 块编写相同的逻辑。使用嵌套的 if 块,使用 = 运算符,这应该有助于降低复杂性。另外,考虑对这种方法进行单元测试
标签: c# asp.net datetime gridview