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

date range defaults

4 Answers 122 Views
Filter
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 12 Jul 2012, 10:56 AM
I have implemented the date range filter template with two raddatepicker controls to filter records between a startdate and an enddate.
My table contains records from 2001 til 2012. How can I effectively setup defaults so that initially only records from say 2011-2012 are shown ? I have set the startdate control to 01-01-2011 but of course the date range values are only added to the filterexpression when a user selects a different date value.

Regards,
Robert

4 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 16 Jul 2012, 03:08 PM
Hello Robert,

Thank you for contacting us.

You could apply filtering on initial page load as shown below:
Copy Code
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridColumn col = RadGrid1.MasterTableView.GetColumnSafe("OrderDate") as GridColumn;
GridFilteringItem filterItem = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;
col.CurrentFilterValue = DateTime.Now.Date.ToString();
filterItem.FireCommandEvent("Filter", new Pair("EqualTo", "OrderDate"));
if (RadGrid1.Items.Count == 0)
{
RadGrid1.MasterTableView.FilterExpression = string.Empty;
RadGrid1.MasterTableView.Rebind();
}
}
}

An alternative approach would be the following:
  mark-up:
Copy Code
<telerik:RadGrid ... EnableLinqExpressions="false" OnPreRender="RadGrid1_PreRender">
  C#:
Copy Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RadGrid1.MasterTableView.FilterExpression = "[OrderDate]='" + DateTime.Now.Date.ToString() + "'";
}
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
if (!IsPostBack && RadGrid1.Items.Count == 0)
{
RadGrid1.MasterTableView.FilterExpression = string.Empty;
RadGrid1.MasterTableView.Rebind();
}
}
  
Please note that in spite of "EqualTo", you will need to use "Between" filter function according to your specific scenario. Please refer to the topic below for additional details on filter expressions:
 Operating with the FilterExpression of Telerik RadGrid Manually

Additionally, you could check out the following demo which demonstrates how to preserve the text in a Filter Template with two date pickers:
 Grid / Filter Templates

I hope this will prove helpful.

Greetings,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Robert
Top achievements
Rank 1
answered on 18 Jul 2012, 08:14 AM
**updated: never mind: was a globalization issue**

Thanks, I will try your suggestion.
In the meantime I have a different -strange- problem.
I customized the filtertemplate example and included the daterange filter, which works fine.
That is: if I compile and run the project inside VS, using the buildin localhost webserver.
Now when I publish the precompiled website to our local intranet, the daterange filter does not filter anything, all records are displayed, and the loading panel forever keeps on showing the rotating cursor...... Any suggestion where to search for this problem ?

Kind regards,
Robert
0
Robert
Top achievements
Rank 1
answered on 18 Jul 2012, 12:16 PM
Eyup,

Your suggestion works, but only on the initial pageload.
As soon as we select a value from one of the dropdownlists of other filtercolumns the default filtercondition for our daterange filter is ignored.

Kind regards,
Robert

Code:



Private
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

If Not IsPostBack Then

 RadGrid1.MasterTableView.FilterExpression = "(([T$QDAT] >= '01/01/2011') AND ([T$QDAT] <= '31/12/2015'))"

End If

End Sub

 

Protected Sub RadGrid1_PreRender(ByVal source As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender


If
(Not IsPostBack And RadGrid1.Items.Count = 0) Then 
   RadGrid1.MasterTableView.FilterExpression = String.Empty

   RadGrid1.MasterTableView.Rebind()

 

   RefreshCombos()

 

End If

 

 

 

 

 

 

End Sub

0
Eyup
Telerik team
answered on 20 Jul 2012, 01:02 PM
Hi Robert,

In order to preserve the filtered value, please try the following:
  protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        RadGrid1.MasterTableView.FilterExpression = "([ShipCountry] LIKE \'%Germany%\') ";
        GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("ShipCountry");
        column.CurrentFilterFunction = GridKnownFunction.Contains;
        column.CurrentFilterValue = "Germany";
        RadGrid1.MasterTableView.Rebind();
    }
}

As demonstrated in the demo below:
 Applying Default Filter on Initial Load

That should solve the issue.

Kind regards,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Filter
Asked by
Robert
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Robert
Top achievements
Rank 1
Share this question
or