Filtering feature can be added or removed programmatically.
In this topic, you will learn how to programmatically add and remove filter in WebGrid.
To add filter programmatically in WebGrid
Server Side
- Drag a standard button to your WebForm.
- On ButtonClick event handler, put the following code:
C# Copy Code private void Button1_Click(object sender, System.EventArgs e) { WebGridFilter fltr = new WebGridFilter("ContactName", ColumnFilterType.Like, "Ana"); WebGrid1.RootTable.FilteredColumns.Add(fltr); }
- Run the Project.
Client Side
- Drag a HTML button to your form.
- Create AddFilter() functions in client-side:
JavaScript Copy Code function AddFilter() { var grid = ISGetObject("WebGrid1"); var newFilter = new WebGridFilter(); newFilter.ColumnMember = "ContactName"; newFilter.FilterType = "Like"; newFilter.FilterText = "Ana"; grid.RootTable.FilteredColumns.Add(newFilter); grid.RootTable.UpdateUI(); grid.Refresh(); }
- Invoke AddFilter() function in <INPUT type="button" value="Add Filter" onclick="AddFilter()">
- Run the Project.
To remove filter programmatically in WebGrid
Server Side
- Drag a standard button to your WebForm.
- On ButtonClick event handler, put the following code:
C# Copy Code private void Button2_Click(object sender, System.EventArgs e) { WebGrid1.RootTable.FilteredColumns[0].FilterType = ColumnFilterType.NotSet; WebGrid1.RootTable.FilteredColumns[0].FilterText = ""; }
- Run the Project.
Client Side
- Drag a HTML button to your form.
- Add RemoveFilter() functions in client-side:
JavaScript Copy Code function RemoveFilter() { var grid = ISGetObject("WebGrid1"); grid.RootTable.FilteredColumns.Remove(grid.RootTable.FilteredColumns[0], true); grid.RootTable.FilteredColumns[0].FilterType = ""; grid.RootTable.FilteredColumns[0].FilterText = ""; grid.RootTable.UpdateUI(); grid.Refresh(); }
- Call RemoveFilter() function in <INPUT type="button" value="Remove Filter" onclick="RemoveFilter()">.
- Run the Project.
Tasks
How-to: Filter invisible column
References
AllowFilter Property
ButtonClick Event
FilterType Property
FilterText Property
Other Resources
Walkthrough Topics
How-to Topics