SPGridView 中日期/时间的 ObjectDataSource 过滤

发布于 2024-09-26 06:01:01 字数 1545 浏览 5 评论 0原文

我对此感到摸不着头脑...我有日期/时间列,我想按日期/时间排序,但在过滤时,仅按日期/时间的日期部分进行过滤。我的过滤器代码(设置方法中包含的 lambda)如下所示:

        this.taskGrid.AllowFiltering = true;
        this.odsGrid.Filtering += (sender, e) =>
        {
            var odsv = (ObjectDataSourceView)sender;
            var filterExp = odsv.FilterExpression;

            if (filterExp.Contains("Date")
                || filterExp.Contains("Deadline")
                || filterExp.Contains("Client Proof"))
            {
                var fieldAndVal = filterExp.Split('=');
                DateTime date;

                if (DateTime.TryParse(fieldAndVal[1].Replace("'", string.Empty), out date))
                {
                    odsv.FilterExpression = "("
                        + fieldAndVal[0] + " >= '" + date
                        + "') AND ("
                        + fieldAndVal[0] + " < '" + date.AddDays(1) + "')";
                }
            }

            this.ViewState["FilterExpression"] = odsv.FilterExpression;
        };

因此,它的作用是更改看起来像“[Client Proof] = '8/5/2010 4:24:44 PM'”的表达式并重写它为“([客户证明] >= '8/5/2010') AND ([客户证明] < '8/6/2010')”。

现在最关键的是,它可以在我的开发环境(Win2K3 32 位,MOSS 2K7)中运行,但是一旦我将解决方案提升到 QC 环境(Win2K8R2 64 位,MOSS 2K7)中,执行相同的过滤结果会产生空网格。 有人有什么想法吗?或者有一个好方法可以了解应用过滤器时实际发生的情况?谢谢!

ETA:

事实证明,当到达 Filtering 事件时,[{1}] = '{0}' 的过滤器格式已经应用我所做的一切并没有起到任何作用。以前的开发人员将日期视为字符串并使用 LIKE,这种方法工作正常,但排序却很疯狂。因此,作为日期/时间进行排序并像字符串一样进行过滤将是我想要的,但似乎无法将两者结合在一起。

I'm scratching my head on this one... I have date/time columns which I want to sort by date/time but when filtered, only filter by the date part of the date/time. My filter code (a lambda contained within a setup method) looks like this:

        this.taskGrid.AllowFiltering = true;
        this.odsGrid.Filtering += (sender, e) =>
        {
            var odsv = (ObjectDataSourceView)sender;
            var filterExp = odsv.FilterExpression;

            if (filterExp.Contains("Date")
                || filterExp.Contains("Deadline")
                || filterExp.Contains("Client Proof"))
            {
                var fieldAndVal = filterExp.Split('=');
                DateTime date;

                if (DateTime.TryParse(fieldAndVal[1].Replace("'", string.Empty), out date))
                {
                    odsv.FilterExpression = "("
                        + fieldAndVal[0] + " >= '" + date
                        + "') AND ("
                        + fieldAndVal[0] + " < '" + date.AddDays(1) + "')";
                }
            }

            this.ViewState["FilterExpression"] = odsv.FilterExpression;
        };

So what this does is changes an expression which looks like "[Client Proof] = '8/5/2010 4:24:44 PM'" and rewrites it as "([Client Proof] >= '8/5/2010') AND ([Client Proof] < '8/6/2010')".

Now here's the kicker, it works in my development environment (Win2K3 32-bit, MOSS 2K7) but once I promote the solution into the QC environment (Win2K8R2 64-bit, MOSS 2K7), performing the same filter results in an empty grid. Does anyone have any ideas? Or a good way to see what's actually happening when the filter is applied? Thanks!

ETA:

As it turns out, by the time it gets to the Filtering event, the filter format of [{1}] = '{0}' has already been applied and all that stuff I'm doing isn't doing anything. The previous developers treated the date as a string and used LIKE, which worked all right but then sorting was bonkers. So, sorting as a date/time and filtering like a string would be what I'd like but can't seem to bring the two together.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后彩虹 2024-10-03 06:01:01

过了一会儿我想到了一件事。您能否生成一个仅包含完整日期时间列中的日期部分的新列?然后,您可以将过滤器应用到仅包含日期部分的列。

您可以尝试将 BoundField.DataField 设置为 DateOnly 列。

PS 不确定为什么它在一种环境中工作而在另一种环境中不起作用。我猜是数据问题/差异。

I thought of something after a while. Could you generate a new column that contains only the date portion from the full DateTime column? Then, you would apply the filter to the column that contains only the date portion.

You might try setting the BoundField.DataField to your DateOnly column.

P.S. Not sure why it is working in one environment and not in another. I'm guessing a data problem / difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文