Filter using database

8 posts, 0 answers
  1. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 12 Oct 2010 Link to this post

    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
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 12 Oct 2010 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 12 Oct 2010 Link to this post

    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
  5. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 12 Oct 2010 Link to this post

    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
  6. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 12 Oct 2010 Link to this post

    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
  7. Vassil Petev
    Admin
    Vassil Petev avatar
    1765 posts

    Posted 13 Oct 2010 Link to this post

    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
  8. Ramin Naderi
    Ramin Naderi avatar
    5 posts
    Member since:
    Apr 2010

    Posted 25 Jun 2013 Link to this post

    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.
  9. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 28 Jun 2013 Link to this post

    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 >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready