Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
80
Webdatagrid - Date filtering not possible
posted

I have a webdata grid with multiple columns, including a date column that displays the date and time:

<ig:BoundDataField DataFieldName="JobImportTime" Key="JobImportTime" DataFormatString="{0:dd.MM.yyyy HH:mm}" DataType="System.DateTime" Header-Text="ImportTime" />

The date and time is correctly displayed. Unfortunately, filtering with filter condition "Equals" does not work. Before, After, Yesterday, and so forth work great, but not Equals since I want to filter only for a specific date.

To make this clear, here is an example:

The grid displays "28.08.2018 17:23". Now I filter for "28.08.2018", but the grid says, that there isn't such a record. I tried to achieve this behaviour by converting the RuleDateNode to TextNode, but without luck:

private void Filtering_DataFiltering(object sender, FilteringEventArgs e)
{
    RuleDateNode ruleTextNode = (RuleDateNode)e.ColumnFilters[0].Condition;
    RuleTextNode ruleText = new RuleTextNode(TextFilterRules.Contains, ruleTextNode.Value.ToShortDateString());
    e.ColumnFilters.Clear();
    
    ColumnFilter newFilter = new ColumnFilter(((WebDataGrid)sender), "JobImportTime");
    newFilter.Condition = ruleText;
    e.ColumnFilters.Add(newFilter);
}

I get the error message, that it  is not possible to apply a text filter to a columnt with type "DataNode".

Any help is very appreciated.

Parents
  • 6365
    Offline posted

    Hello Flo,

    Thank you for the code-snippet you have provided.

    The behavior you have described in regards to not having results when filtering by date only is expected.
    The values of the DateTime cells of the records are DateTime objects with both date and time (for example: 28.08.2018 17:23).
    When the filtering value is set only as date (for example: 28.08.2018), then the DateTime struct itself uses the default time value by making the filter value 28.08.2018 00:00.

    In this case since 28.08.2018 17:23 does not equal to 28.08.2018 00:00, the record will not be included in the filter results.

    In other to successfully filter by the necessary criterion, I can suggest you the following approaches, depending on your scenario:

    1. Filter by both Date and Time (not Date only)
    If we filter by both Date and Time, then all the records with the same date and time will be displayed. We can do this by using a DateTimeEditorProvider (refer to the CreateDateTimeEditorProvider() method in the attached sample).

    2. Define an unbound column, that uses the same DateTime values by extracting only their dates. This way if we filter by this unbound column, only results with the respective date will appear.

    3. Define a hidden unbound column, that uses the same DateTime values by extracting only their dates. This way we can handle the Filtering event of the data grid and change the ColumnKey of the ColumnFilter object from the the original DateTime column to be the hidden unbound DateTime column. This way we will filter by one column, but the filtering will be executed on the other one, which has Date values only. (Note that in this case you will have to manually clear the filtering through the API.)

    protected void dataGrid_DataFiltering(object sender, FilteringEventArgs e)
    {
    ColumnFilter filter = e.ColumnFilters[0];
    RuleDateNode ruleTextNode = (RuleDateNode)filter.Condition;
    
    if (filter.ColumnKey == "Shipping" && ruleTextNode.Rule == DateTimeFilterRules.Equals)
    {
    filter.ColumnKey = "ShippingDate";
    }
    }

    I have attached a sample application that demonstrates approaches 1. and 2. from above.
    (Please note that you might have to change the assembly references and the stylesSetPath property in the Web.config so they target your specific version and path for the styles.)

    If you have any questions, please let me know.

    0638.WebDataGrid_sampleApp.zip

Reply Children
No Data