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

Rebind datasource on filter change

2 Answers 272 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris Ward
Top achievements
Rank 1
Chris Ward asked on 06 Jun 2012, 06:06 PM
I have a large set of data with which server-side paging is implemented.  I need to requery the data source any time the GridView's filter is changed by the user.  The problem lies in the fact that rebinding the DataSource inside any of the FilterChanged, FilterChanging, or FilterExpressionChanged events of the grid causes a null reference exception at the application level.

To work around this, I have found the following code which will correctly refresh the data source after a filter change has been finalized.

private void TicketView_CellEndEdit(object sender, GridViewCellEventArgs e)
{
    if ((sender as GridViewEditManager).GridViewElement.CurrentCell is GridFilterCellElement)
    {
        RequeryGridData();
    }
}

While the data source now refreshed automatically on a text change, it still did not refresh on a filter type change (ie. "Contains" to "Does not contain").  I tried calling RequeryGridData() in the closed event of the filter popup menu as follows.

private void TicketView_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    if (e.ContextMenuProvider is GridFilterCellElement)
    {
        e.ContextMenu.DropDownClosed +=
        new RadPopupClosedEventHandler(FilterContextMenu_Closed);
    }
}
 
void FilterContextMenu_Closed(object sender, EventArgs e)
{
    RequeryGridData();
}

This still caused a null reference exception.  I wondered what would happen if I delayed the datasource refresh until after the close event had finished and found myself with this code.

private void TicketView_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
{
    if (e.ContextMenuProvider is GridFilterCellElement)
    {
        e.ContextMenu.DropDownClosed +=
        new RadPopupClosedEventHandler(FilterContextMenu_Closed);
    }
}
 
void FilterContextMenu_Closed(object sender, EventArgs e)
{
    //HACK: Have to refresh grid contents, but MUST NOT do it from a filter changing event as
    //      updating the datasource while the filter change is finalized crashes the program
    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
    timer.Interval = 1;
    timer.Tick += new EventHandler(FilterChangedTimer_Tick);
    timer.Start();
}
 
void FilterChangedTimer_Tick(object sender, EventArgs e)
{
    (sender as System.Windows.Forms.Timer).Dispose();
    RequeryGridData(); 
}

Which, as I suspected, works great.  No exceptions occurred.

My question is: Is there a cleaner way of detecting the filter change and performing a datasource change?  Perhaps this functionality is not the intended use of the GridView's filter controls, in which case I'll settle for the solution (read: hack) I've managed to come up with, but hopefully there is a better way to handle this.

Thank you for your time.

2 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 11 Jun 2012, 03:09 PM
Hello Chris,

Currently, RadGridView control does not support this functionality and changing the data source in the filtering events can produce unexpected results and unstable state of the grid control. I logged your scenario as a feature request in our Public Issue Tracking System. Feel free to add your vote for it here: http://www.telerik.com/support/pits.aspx#/public/winforms/11402. I am not able to provide you with a workaround due to the nature of the data processing. 

Let us know if you have any other questions.

All the best,
Julian Benkov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Chris Ward
Top achievements
Rank 1
answered on 11 Jun 2012, 03:13 PM
I understand, thank you for your reply.
Tags
GridView
Asked by
Chris Ward
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Chris Ward
Top achievements
Rank 1
Share this question
or