Search code examples
.netwinformsdevexpressxtragrid

Devexpress: Clearing bound controls of completely filtered GridControl


I have a Devexpress.XtraGrid.GridControl that I have set the DataSource to a DataTable. I also have several other controls that have databindng to the same DataTable. This works out well so that when the selected row of the GridView changes, the controls reflect the change.

However, on the GridControl's GridView I have set the ActiveFilter.NonColumnFilter. This filter changes as the form is used. Occasionally, the filter gets set to something that makes no rows show up in the GridControl. When this happens, the controls that are bound to the DataTable do not clear, but instead show the data from the last selected row.

Is there something simple I can do to tell the bound controls to clear, or do I need to manually go through them all and clear them?

I had previously used a DataTable that actually added and removed rows appropriate to what I was filtering. If the table became clear, all of the bound controls did as well. So I'm assuming it is possible.


Solution

  • I contacted DevExpress about this issue and received a response:

    Thank you for your question. When the grid is completely filtered and contains no rows, it cannot set the current item in the binding source.

    To solve this problem I suggest you filter data on the datasource level.

    Using this information, I changed from using the GridView.ActiveFilter.NonColumnFilter for my filtering needs and instead used the DataTable.DefaultView.RowFilter. That appears to have solved my issue.

    This was different from the example they gave me:

    BindingSource bs = new BindingSource();
    DataView dv = new DataView();
    
    DataTable dt = CreateTable(10);
    dv = new DataView(dt);
    bs.DataSource = dv;
    
    gridControl1.DataSource = bs;
    textEdit1.DataBindings.Add("EditValue",bs,"Name");
    textEdit2.DataBindings.Add("EditValue", bs, "ID");
    

    You can then call this to set the filter:

       dv.RowFilter = "ID = 50";