public class MainActivity : Activity
{
SfDataGrid sfGrid;
FilteringViewModel viewModel;
SearchView filterText;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
LinearLayout linear = new LinearLayout(this);
linear.Orientation = Orientation.Vertical;
sfGrid = new SfDataGrid(this);
sfGrid.ColumnSizer = ColumnSizer.Star;
viewModel = new FilteringViewModel(this);
viewModel.SetRowstoGenerate(100);
sfGrid.ItemsSource = new GridDataTableCollectionViewExt(this.sfGrid, (IEnumerable)viewModel.DataTable.DefaultView);
filterText = new SearchView(this);
filterText.SetQueryHint("Enter the Text to filter");
filterText.QueryTextChange += OnFilterTextChanged;
viewModel.filtertextchanged = OnFilterChanged;
linear.AddView(filterText);
linear.AddView(sfGrid);
SetContentView(linear);
}
void OnFilterChanged()
{
if (sfGrid.View != null)
{
var rowfilter = string.Empty;
for (int i = 0; i < this.sfGrid.Columns.Count; i++)
{
var colfilter = string.Empty;
colfilter = colfilter.Predicate(this.sfGrid.Columns[i].MappingName, filterText.Query, FilterType.Contains);
if (string.IsNullOrEmpty(rowfilter))
rowfilter = "(" + colfilter + ")";
else
rowfilter = rowfilter.OrPredicate() + "(" + colfilter + ")";
}
(this.sfGrid.View as DataTableCollectionView).ViewSource.RowFilter = rowfilter;
this.sfGrid.View.RefreshFilter();
}
}
void OnFilterTextChanged(object sender, SearchView.QueryTextChangeEventArgs e)
{
viewModel.FilterText = (sender as SearchView).Query.ToString();
}
}
public class GridDataTableCollectionViewExt : GridDataTableCollectionViewWrapper
{
public GridDataTableCollectionViewExt(SfDataGrid dataGrid, IEnumerable source)
: base(source, dataGrid)
{
}
public override void RefreshFilter(bool isProgrammatic)
{
}
} |