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

[Solved] How can I filter a column programatically?

1 Answer 138 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ryan
Top achievements
Rank 1
Ryan asked on 21 Jan 2011, 05:03 AM

Hi,
I would like to filter a column of the grid right after I bind it to a datasource, and would like to keep the option for the user to clear the filter and see all the data.
Here is the code that I am using to filter out results:

foreach (GridViewDataColumn col in radGridView.Columns.OfType<GridViewDataColumn>())
{
    if (col.UniqueName.ToLower() == "owner")
     {
          FilterOperator filterOperator = FilterOperator.Contains;
          FilterDescriptor descriptor = new FilterDescriptor(col.UniqueName, filterOperator, "ryan");
          descriptor.MemberType = col.DataType;
          radGridView.FilterDescriptors.Add(descriptor);
          break;
    }
}

There are really two questions here:
1. How can I filter the same column with more than one value? In other words I want to see rows where (owner == ryan) OR (owner==bill).
2. When I run the code, I do the rows filtered out, however when I click on the filter for the column I don't get the list of the other owners. In other words, the user cannot clear the filter and see all rows. What is the right of doing that?

Thanks,
Ryan

1 Answer, 1 is accepted

Sort by
0
Accepted
Milan
Telerik team
answered on 21 Jan 2011, 08:13 AM

Hello Ryan,

Using ColumnFilterDescriptor is the solution to both of the problems. You can use it in the following way:

ColumnFilterDescriptor columnDescriptor = new ColumnFilterDescriptor((IDataFieldDescriptor) this.playersGrid.Columns[2]);
            columnDescriptor.DistinctFilter.DistinctValues.Add("ryan");
            this.playersGrid.FilterDescriptors.Add(columnDescriptor);

More information about this type of descriptor can be found here.

Hope this helps.



Best wishes,
Milan
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
GridView
Asked by
Ryan
Top achievements
Rank 1
Answers by
Milan
Telerik team
Share this question
or