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

Filter using database

7 Answers 114 Views
GridView
This is a migrated thread and some comments may be shown as answers.
SUNIL
Top achievements
Rank 2
Iron
SUNIL asked on 12 Oct 2010, 03:18 PM
I would like to go to the database every time a user inputs a filter and then clicks on the filter button.
So 2 things are different here.
  1. Filter does not get applied as user types in the filter, but only when user clicks on the filter button for that column.
  2. When user clicks on the filter button, then a new result set is returned from database based on filter condition. The results of this filter query will re-bind the database.

Thanks

7 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 12 Oct 2010, 03:29 PM
Hi, 

Emanuel helped someone to filter onLeave of the filter with this excellent solution. See this forum post
http://www.telerik.com/community/forums/winforms/gridview/filtering-question.aspx

You can adapt this to go back to the database and rebind the data based on the filter. 
Hope that helps
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 12 Oct 2010, 04:58 PM
Hello Sunil, Richard

Richard is right, for the first part you can use my answer from there, and thank you very much Richard for mentioning me.

The second part is the difficult one, because you are in a Cell event, you cannot change the data source for the grid without it crashing, so the first thing that came to mind is to use a Timer, and get the data on Tick, and set it as a DataSource to the grid there.

If you decide to go with this version, please be careful to either use a System.Windows.Forms.Timer because this one is running on the MainThread or use a synchronization context, or invoke using delegates to avoid CrossThreadOperation Exceptions.

The example should be something like this:
void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e)
{
    var filteringRow = e.Row as GridViewFilteringRowInfo;
    if (filteringRow != null)
    {
        if (lastFilterDescriptor != null)
        {
            var propertyName = lastFilterDescriptor.PropertyName;
            var filterOperator = lastFilterDescriptor.Operator;
            var filterValue = lastFilterDescriptor.Value.ToString();
 
            switch (filterOperator)
            {
                case FilterOperator.Contains:
                    ChangeDataSource(true, filterValue);
                    break;
                case FilterOperator.NotContains:
                    ChangeDataSource(false, filterValue);
                    break;
                case FilterOperator.EndsWith:
                    break;
                case FilterOperator.IsContainedIn:
                    break;
                case FilterOperator.IsEqualTo:
                    break;
                case FilterOperator.IsGreaterThan:
                    break;
                case FilterOperator.IsGreaterThanOrEqualTo:
                    break;
                case FilterOperator.IsLessThan:
                    break;
                case FilterOperator.IsLessThanOrEqualTo:
                    break;
                case FilterOperator.IsLike:
                    break;
                case FilterOperator.IsNotContainedIn:
                    break;
                case FilterOperator.IsNotEqualTo:
                    break;
                case FilterOperator.IsNotLike:
                    break;
                case FilterOperator.IsNotNull:
                    break;
                case FilterOperator.IsNull:
                    break;
                case FilterOperator.None:
                    break;
                case FilterOperator.StartsWith:
                    break;
                default:
                    break;
            }
        }
    }
}
 
private void ChangeDataSource(bool containsString, string stringToContain)
{
    radGridView1.EndEdit();
    var timer = new System.Windows.Forms.Timer();
    timer.Interval = 1;
    timer.Tick += delegate
    {
        timer.Stop();
        // if by any chance it takes more than 1ms you could make the timer thread wait while the grid finished validating
        //while (radGridView1.IsInEditMode)
        //{
        //    Thread.Sleep(20);
        //}
 
        // get data from DB based on these values
        radGridView1.DataSource = new BuyersCollection(containsString, stringToContain);
 
    };
 
    timer.Start();
}

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

*Update - sorry for the first sentence in the original message, i forgot to clean it up

Best Regards,
Emanuel Varga
0
SUNIL
Top achievements
Rank 2
Iron
answered on 12 Oct 2010, 07:14 PM
Hi Emanuel,

Thanks for your answer.
My only problem is that I am using Q1 2008 SP1 version in my app.
So this code may not work for me.

Thanks
Sunil
0
Emanuel Varga
Top achievements
Rank 1
answered on 12 Oct 2010, 07:28 PM
Hello Sunil,

Best chances are you are right about that, if possible i would strongly suggest you update to the latest version, and the reasons for this are many.
I'm not even going to bring up the recent virtualization and strong performance boosts in the main controls section.
But there are also smaller reasons, like the fact that chances are slim to none that you will be find a lot of people with that version and who are willing to help. (I don't even think that the admins still have that version installed somewhere - but i might be wrong).

If you have any other questions i can help you with, please, do not hesitate to say so.

Best Regards,
Emanuel Varga
0
Vassil Petev
Telerik team
answered on 13 Oct 2010, 04:43 PM
Hi Sunil,

When posting in our Forums please share your product version, especially if it is not the latest version. This information is vital for everyone who would like to assist you.

We will greatly appreciate your assistance in this.
 

Regards,
Vassil
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Ramin Naderi
Top achievements
Rank 1
answered on 25 Jun 2013, 11:17 AM
Hi
I have some code like this:

private Telerik.WinControls.Data.FilterDescriptor lastFilterDescriptor;
 private void GridControl_FilterChanging(object sender, GridViewCollectionChangingEventArgs e)
        {
            if (GridControl.IsInEditMode)
            {
                e.Cancel = true;
            }
            lastFilterDescriptor = ((Telerik.WinControls.Data.FilterDescriptor)e.NewItems[0]);
 }
 
private void GridControl_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            var filteringRow = e.Row as GridViewFilteringRowInfo;
            if (filteringRow != null)
            {
                GridControl.FilterDescriptors.Remove(e.Column.Name);
                 
                if (lastFilterDescriptor != null && lastFilterDescriptor.PropertyName == e.Column.Name)
                {
                    _timer.Interval = 10;
                    _timer.Tick += delegate
                    {
                        _timer.Stop();
                       GridControl.DataSource=GetDataSource();
                       GridControl.FilterDescriptors.Add(lastFilterDescriptor);
                   };
                    _timer.Start();
                }
            }
        }
Every time i type a word in filter row, my program work decussate.
In other words, bellow code does not work correctly:

private void GridControl_CellEndEdit(object sender, GridViewCellEventArgs e)
{
    var filteringRow = e.Row as GridViewFilteringRowInfo;
    if (filteringRow != null)
    {
        GridControl.FilterDescriptors.Remove(e.Column.Name);
        if (lastFilterDescriptor != null && lastFilterDescriptor.PropertyName == e.Column.Name)
        {
            GridControl.FilterDescriptors.Add(lastFilterDescriptor);
        }
    }
}
Excuse my bad english.
0
Ivan Petrov
Telerik team
answered on 28 Jun 2013, 11:52 AM
Hello Ramin,

Thank you for writing.

I tested the code you have provided and everything worked correctly. I just copy-pasted your code and set a data source. I tested the code with our latest version which currently is Q2 2013. If you are using a different version I would kindly ask you to post the version of the controls you are using so I can test the code with it. 

Looking forward to your reply.

Regards,
Ivan Petrov
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
SUNIL
Top achievements
Rank 2
Iron
Answers by
Richard Slade
Top achievements
Rank 2
Emanuel Varga
Top achievements
Rank 1
SUNIL
Top achievements
Rank 2
Iron
Vassil Petev
Telerik team
Ramin Naderi
Top achievements
Rank 1
Ivan Petrov
Telerik team
Share this question
or