A common need for web-based reporting is to create a UI to allow the user to apply filter criteria to a set of data. Usually, this involves coding a fairly complicated UI, and associated code to apply the filtering logic to a DataView. This UI usually is fairly static, possibly containing a drop down list to allow the user to choose which column to filter by, UI elements to allow them to select the filter criteria, and a mechanism to transform this data into a RowFilter to apply to a data view. This can become tedious to code and maintain over time. Iâve created a custom control that can do all this for you, which Iâve been able to use to allow numerous web-based reporting applications to be rapidly developed here at the port.
What is it?
This WebControl, DataSetFilter, builds this complicated UI on the fly, given a particular DataSet and DataTable. It does the following:
Grabs the DataTableâs columns and each columnâs types, and generates a drop down list, which the user can use to choose the column to sort by.
Adds the appropriate operator for the columnâs type. For example LIKE is only appropriate for string types, greater than only for numeric and DateTime types.
Adds the appropriate value entering control. For example, for DateTime types, it adds a calendar control, for other types it adds a textbox.
If multiple filters are added, it adds a boolean drop down list, to allow the next filter to be properly added.
Finally, it raises an event when the filter has changed, so that you can handle this event, normally applying it to your DataView, and re-binding your DataGrid.
So, for example, hereâs what it looks like when applied to Sahilâs Animal DataSet.
And to see all animals that weight greater than 15 pounds and less than 5 pounds, weâd add two filters. Notice the Boolean drop down list.
For DateTime values, it presents a Calendar picker.
Combine this with a standard ASP.NET DataGrid, and youâve got a quick, but powerful reporting tool.
What do you need to code to get it to work?
Thereâs really not much that you need to do, just plop an instance of the DataSetFilter on to a form, DataBind it, and handle the FilterChanged event. Hereâs all the code that you need to bind the DataSetFilter.
Thereâs some other stuff in my Tompkins.Web namespace to enable this control, such as my CalendarPlus control, but everything you need is there. Thereâs also support for adding parameters, which can prompt the end user for values to be entered, in the case of a stored filter. Iâll try to blog about how all that works next time.