views:

75

answers:

1

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.

+1  A: 

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.

Kit Menke
This was suggested once and I thought I'd found a better way. Let me give this a run and I'll let you know how it turns out.
Jesse C. Slicer
Well, so far, I have gotten the exact same results with the new method. This is quite perplexing. The data problem/difference is quite likely, but since the environment I can debug is the one that works, this will make it quite difficult.
Jesse C. Slicer
I'm not sure how much flexibility you have, but a quick method I've used is to simply dump your DataTable (not sure how you are retrieving the data) to your grid so that you can see the data. Any way you can query the source?
Kit Menke
Well, the data is visible in the grid *before* the filter operation. The DataTable is built up from columns from views on two different SharePoint lists (Workflow Tasks and our document library) and several web parts on the page show different views of this. Each web part contains the grid in question. In this particular case, my QC environment, there are 5 records which show in the grid pre-filter and then filtering by any one of the dates makes the grid go away. Filtering by any other column works as expected.
Jesse C. Slicer
Ok, I'm seeing a pattern now. If the column's actual data is XX/YY/ZZZZ 12:00 AM, then my filter will work. But if the time portion is anything else, it won't. At least now I have reproducible success and failure cases.
Jesse C. Slicer
Because of previous comment, it turns out that my blob of code in the `Filtering` event handler is doing NOTHING. The filtration is taking place as a pure equals from my filter drop-down choice to those column values. Seems like I'm right back at the beginning.
Jesse C. Slicer
I haven't been able to get the DateTime vs Date thing working yet.. but to update the FilterExpression you'll need to make a code change. In other webparts I have a CompositeControl that handles creating the SPGridView and ObjectDataSource. In order to successfully change the ODS FilterExpression, I updated it in the CompositeControl's OnPreRender. See here: http://kitmenke.com/blog/2009/09/05/sharepoints-spgridview-filtering-and-apostrophes/
Kit Menke
Also, did you get the values that display in the filter dropdown to display only the date values? Or does it display all the unique date and time values?
Kit Menke
I use a chunk of code that knows it's getting the filter postback and gets the unique date (plus 12:00:00AM time) in the list.
Jesse C. Slicer
Your `OnPreRender` looks like it might be doing the trick. I just got it to work in a couple of instances, but also scored myself an exception elsewhere. So if I nail that down, we're in business!
Jesse C. Slicer
And the final update - WIN! Thanks for the assistance there. My hair can start regrowing now.
Jesse C. Slicer
Great! Glad you were able to get it working. I might have to do a follow up blog post with the complete solution, since I was finally able to get it working thanks to Reflector.
Kit Menke
Were would the world be without Reflector?
Jesse C. Slicer