Client Side Filtering
Once the data has been retrieved from the server, it can be filtered in the client without sending any requests to the server.
Open MainForm
again in Designer mode and add a new textfield filterTextBox
and a button applyFilterButton
that will apply the filter.
Double click on the applyFilterButton
to add the click event handler and then add the following code.
private void applyFilterButton_Click(object sender, EventArgs e)
{
String filterText = this.filterTextBox.Text;
if (String.IsNullOrEmpty(filterText))
{
this._dataSource.DataSource = this._data;
}
else
{
this._dataSource.DataSource = this._data.Where(task => task.Task.Contains(filterText) || task.Details.Contains(filterText));
}
}
We first retrieve the text from the filterTextBox
and test to see whether there is a value or not. If there isn't we show the original data again (effectively clearing the filter). Otherwise we create a new data source where elements match our filter text in either the task name or task description.
Run the app again
Enter some text in the filter text box, here we use "NoMatch" and the press the apply filter button. As you can see no match is found.
Change the text in the filter text box to something we know is in a task, here the text ID
The only thing left to do is summarize what we've done.