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

Filtering Question

5 Answers 225 Views
GridView
This is a migrated thread and some comments may be shown as answers.
DDM Consulting
Top achievements
Rank 2
DDM Consulting asked on 07 Oct 2010, 02:04 PM
How can I keep the gridview from applying its filterdescriptor untill the user presses enter in the filtercell (or leaves the cell by tab or click ie: the cellEndEdit event has passed (with rowIndex == -1))?

eg:
i have a column and I enter the filter string "abc" by typing the 3 character. Normally the grid will first add the fitlerdescriptor A  then AB then ABC. Now  I would like the behaviour of my gridview to be abc + "LEAVES FILTERFIELD" and then apply only 1 filterdescriptor ABC..

I have tried using a boolean "holdFiltering" which i set to true when the cellBeginEdit is fired on RowIndex == -1  and then i set it back to false at cellEndEdit, but unfortunately the cellEndEdit is fired AFTER the FilterChanging event (where i used the holdFiltering to set the e.Cancel = true & return from the method), this resulted in no more filtering whatsoever ;-)

Can anyone point me to or show me a working code snippet?

thank you in advance!

5 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 02:46 PM
Hello DDM Consulting,

Please try the following:
private FilterDescriptor lastFilterDescriptor;
 
void radGridView1_FilterChanging(object sender, GridViewCollectionChangingEventArgs e)
{
    if (radGridView1.IsInEditMode)
    {
        if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            lastFilterDescriptor = null;
        }
        else
        {
            lastFilterDescriptor = e.NewItems[0] as FilterDescriptor;
        }
 
        e.Cancel = true;
    }
}
 
void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
{
    var filteringRow = e.Row as GridViewFilteringRowInfo;
    if (filteringRow != null)
    {
        radGridView1.FilterDescriptors.Remove(e.Column.Name);
        if (lastFilterDescriptor != null)
        {
            radGridView1.FilterDescriptors.Add(lastFilterDescriptor);
        }
         
    }
}

I was thinking the following: hold the last descriptor as a local field and cancel the filter changing event, and on CellEndEdit just apply the last filter stored.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
DDM Consulting
Top achievements
Rank 2
answered on 07 Oct 2010, 03:12 PM

Emanuel,

Thank you for your quick reply!
it works very well!

KR

0
Umut
Top achievements
Rank 1
answered on 22 Aug 2011, 01:57 AM
Its work
0
Jason Parrish
Top achievements
Rank 1
answered on 12 Dec 2011, 11:42 PM
It appears on the latest release (q3 2011) that NewItems is not updated properly.  NewValue updates correctly.  Is anybody else seeing the same problem?
0
Stefan
Telerik team
answered on 15 Dec 2011, 04:35 PM
Hello Jason,

Thank you for noticing this. 

We are already aware of this case and it is logged into our PITS system. Here you can find a link to the item, where you can add your vote for it.

Meanwhile, here is how you can achieve the same effect - to filter only when Enter key is press. First, create two variables lastFilterDescriptor of type FilterDescriptor and another one of type Boolean, let's call it isRealFiltering. Subscribe to the FilterChanging event handler, where you check weather the isRealFiltering is true and if so, you will change it back to false, alternatively, cancel the filtering. Now, in order to capture the Enter key press and set the isRealFiltering variable to true, you need to subscribe to the CellEditorInitialized event of RadGridView, get the editor and subscribe to the KeyDown event of the editor element.

Attached you can find a sample project with the above scenario implemented. Please note that you need to instantiate and subscribe to the KeyDown event for all editors used in your grid. 

I hope that you find this information helpful.
 
Best wishes,
Stefan
the Telerik team

Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.

Tags
GridView
Asked by
DDM Consulting
Top achievements
Rank 2
Answers by
Emanuel Varga
Top achievements
Rank 1
DDM Consulting
Top achievements
Rank 2
Umut
Top achievements
Rank 1
Jason Parrish
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or