This is a migrated thread and some comments may be shown as answers.

FilterExpression not woring with others

7 Answers 103 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Smiely
Top achievements
Rank 1
Smiely asked on 22 Dec 2010, 05:49 PM
Thanks Telerik team for replying on my previous thread. Somehow I lost it so opening a new one. Thanks for sending me the exact solution to my problem. I have started working towards the example you send me. http://www.telerik.com/help/aspnet-ajax/grdcustomoptionforfiltering.html 

It's throwing an error "Object reference not set to an instance of an object." on "string[] values = tbPattern.Text.Split(' ');". Don't know why ??

if (e.CommandName == RadGrid.FilterCommandName)
            {
                Pair filterPair = (Pair)e.CommandArgument;
                if (filterPair.Second.ToString() == "DateCreated")
                {
                    string colName = filterPair.Second.ToString();
                    TextBox tbPattern = (e.Item as GridFilteringItem)[colName].Controls[0] as TextBox;
                    string[] values = tbPattern.Text.Split(' ');
                    if (values.Length == 2)
                    {
                        e.Canceled = true;
                        string newFilter = "(([" + filterPair.Second + "] >='" + values[0] + "') AND ([" + filterPair.Second + "] <='" + values[1] + "'))";
                        if (taskGrid.MasterTableView.FilterExpression == "")
                        {
                            taskGrid.MasterTableView.FilterExpression = "('" + startDate1.Value.ToString("MM/dd/yyyy HH:mm:ss") + "' <= [DateCreated] AND [DateCreated] <= '" + endDate1.Value.ToString("MM/dd/yyyy HH:mm:ss") + "')" + newFilter;
                        }
                        else
                        {
                            taskGrid.MasterTableView.FilterExpression += " AND ('" + startDate1.Value.ToString("MM/dd/yyyy HH:mm:ss") + "' <= [DateCreated] AND [DateCreated] <= '" + endDate1.Value.ToString("MM/dd/yyyy HH:mm:ss") + "'  )";
                        }
  
                    }
                }
  
            }
Please help

7 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 27 Dec 2010, 04:43 PM
Hello Smiely,

There are two options for the reason of the null reference exception you are getting.
The first one is that the TextBox tbPatter is null because the (e.Item as GridFilteringItem)[colName].Controls[0] is not a TextBox and the casting returned null.
And the second options is that the tbPatter.Text property of the TextBox might be null and in this case we cannot call the split function.
You may put additional null check to ensure that what is coming from the Controls collection is a TextBox and it has a text entered.

Hope this helps.

Best wishes,
Marin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Smiely
Top achievements
Rank 1
answered on 28 Dec 2010, 07:56 PM
Thanks Marin for replying. I got your point but still confused how to fix it.  (e.Item as GridFilteringItem)[colName].Controls[0] is not a TextBox, it's a GridBoundColumn with To and From datetime as a filter. If I use other filters first and then use datetime, it works fine. But if I filter dates first and then use other filter, it's not appending other filterexpressions to the dates one.

Please help me to solve this problem.....
Thanks
0
Marin
Telerik team
answered on 29 Dec 2010, 01:24 PM
Hello Smiely,

I think the reason the filter expressions are not added if you filter by other columns is that in the code snippet that you posted is actually executed only for the DateCreated column:

if (filterPair.Second.ToString() == "DateCreated")
{
   //...
}

If you wish to append filter expressions when other columns are filtered as well you should handle the other case too:

if (filterPair.Second.ToString() == "OtherColumn1")
{
   //append filter expressions
}
else if (filterPair.Second.ToString() == "OtherColumn2")
{
  //append more filter expressions, etc.
}

Of course you try summarizing and grouping the code if the logic is similar in some cases.

All the best,
Marin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Smiely
Top achievements
Rank 1
answered on 29 Dec 2010, 03:55 PM
Thanks Marin. It does make sense. I am using default filters other than that dateCreated.

taskGrid.MasterTableView.FilterExpression += " AND ('" + startDate1.Value.ToString("MM/dd/yyyy HH:mm:ss") + "' <= [DateCreated] AND [DateCreated] <= '" + endDate1.Value.ToString("MM/dd/yyyy HH:mm:ss") + "'  )"; 

is working. 

"taskGrid.MastertableView.FilterExpression" is getting filter values from other default filters. 

Is there a way appending other filters at the end of the dateCreated custom filter, other than handling all the filters manually. 

If not, how to get a filter value ? I have dropDownLists not Texboxes.

Appreciate your reply.
Thanks,
Smiely

0
Marin
Telerik team
answered on 30 Dec 2010, 01:37 PM
Hello Smiely,

Summarizing the logic for constructing filter expressions strictly depends on your concrete application scenario. For example you can group the logic for extracting the filter value depending on whether you have a textbox in the filter template, a DropDownList or some other control. For the different filter functions (GreaterThen, EqualTo) similar code can also be grouped together. The filter function is the first object in the pair in the command argument for the filter command, and the second one is the column name.
Here isa  sample code snippet on how to extract the filter value from a control in the filter template. You can further group similar logic depending on the filter expressions you would like to construct :

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.FilterCommandName)
        {
            Pair filterPair = (Pair)e.CommandArgument;
            string colName = filterPair.Second.ToString();
            string value="";
            TextBox tbPattern = (e.Item as GridFilteringItem)[colName].Controls[0] as TextBox;
            if ((e.Item as GridFilteringItem)[colName].Controls[0] is TextBox)
            {
                value = ((TextBox)(e.Item as GridFilteringItem)[colName].Controls[0]).Text;
            }
            else if ((e.Item as GridFilteringItem)[colName].Controls[0] is DropDownList)
            {
                value = ((DropDownList)(e.Item as GridFilteringItem)[colName].Controls[0]).SelectedValue;
            }
  
            if (!string.IsNullOrEmpty(value))
            {
                e.Canceled = true;
                      
                //construct filter expression depending on the entered value and filter function (retrieved from filterPair.First)
                if (RadGrid1.MasterTableView.FilterExpression == "")
                {
                    //RadGrid1.MasterTableView.FilterExpression = "...";
                }
                else
                {
                    //RadGrid1.MasterTableView.FilterExpression += " AND ...";
                }
            }
        }
}

Hope this helps, let me know if you have other questions.

Kind regards,
Marin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Smiely
Top achievements
Rank 1
answered on 30 Dec 2010, 07:15 PM
THANKS Marin!! Syntax is working perfectly. I got to know how to get values. I am able to build FilterExpression correctly. 
 
// Working
([Status] = 'CLOSED_ONTIME') AND ('12/30/2010 00:00:00' <= [DateCreated] AND [DateCreated] <= '12/31/2010 00:00:00'  )
  
// NOT Working
('12/30/2010 00:00:00' <= [DateCreated] AND [DateCreated] <= '12/31/2010 00:00:00') AND ([Status] = 'CLOSED_ONTIME')

Then to goes to RadGrid.Rebind(); and result is not correct. Is there anything else is need to set on filters..... like FilterListOption or AutoPostBackOnFilter.  Am I missing something on aspx page?

I have AutoPostBackOnFilter set to false. FilterListOption = VaryByDataType.

Appreciate your help and patience.

Thanks,
smiely 


 


0
Marin
Telerik team
answered on 03 Jan 2011, 12:42 PM
Hello Smiely,

The filtering settings for the grid look ok. You should check whether the filter expressions are passed correctly to the datasource and if it is returning the right data. If not, then the problem is in the execution of the query in the datasource.
If the issue persists I will appreciate if you send a full code sample (including the aspx page) or working project so that I can check it on my side. 

Regards,
Marin
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Calendar
Asked by
Smiely
Top achievements
Rank 1
Answers by
Marin
Telerik team
Smiely
Top achievements
Rank 1
Share this question
or