CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

An ASP.NET Custom Control for Dynamically Filtering Data - DataSetFilter

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 type, and generates a drop down list that the user can use to choose the column to filter.
  • 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.

    public override void DataBind()

    {

      DataSet ds = GetData();

      this.DataSetFilterControl1.DataSource = ds;

      this.DataSetFilterControl1.DataMember = ds.Tables[0].TableName;

 

      DataView dv = new DataView(ds.Tables[0]);

      dv.RowFilter = DataSetFilterControl1.RowFilter;

      lblFilter.Text = DataSetFilterControl1.RowFilter;

 

      this.DataGrid1.DataSource = dv;

      base.DataBind ();

    }

And all that’s left to do is handle the FilterChanged event, and re-bind your DataGrid.  Easy!

    private void DataSetFilterControl1_FilterChanged(object sender, EventArgs e)

    {

      this.DataBind();

    }

Okay. I want it!

You can download the example web project here.

The source code for the DataSetFilter control is here.

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.

-Brendan



Comments

Daniel F said:

Ooh! Ooh! Can't wait to give this a spin
# July 26, 2005 4:58 PM

Christopher Steen - Learning .NET said:

Altova Releases XML Engines To Developers
An ASP.NET Custom Control for Dynamically Filtering Data...
# July 26, 2005 8:53 PM

Darrell said:

Awesome. Good job, Brendan.
# July 27, 2005 5:33 AM

jay reddy said:

while adding Not condition, I am getting this error.
filter is

"Name <> 'Rex' NOT AnimalID = 1002"

Server Error in '/DataSetFilters' Application.
--------------------------------------------------------------------------------

Syntax error: Missing operand after 'AnimalID' operator.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SyntaxErrorException: Syntax error: Missing operand after 'AnimalID' operator.

Source Error:


Line 95:
Line 96: DataView dv = new DataView(ds.Tables[0]);
Line 97: dv.RowFilter = DataSetFilterControl1.RowFilter;
Line 98: lblFilter.Text = DataSetFilterControl1.RowFilter;
Line 99:


Source File: c:\devlmnt\asp.net\datasetfilters\default.aspx.cs Line: 97

Stack Trace:


[SyntaxErrorException: Syntax error: Missing operand after 'AnimalID' operator.]
System.Data.ExpressionParser.Parse()
System.Data.DataExpression..ctor(String expression, DataTable table, Type type)
System.Data.DataExpression..ctor(String expression, DataTable table)
System.Data.DataFilter..ctor(String expression, DataTable table)
System.Data.DataView.set_RowFilter(String value)
DataSetFilters.Default.DataBind() in c:\devlmnt\asp.net\datasetfilters\default.aspx.cs:97
DataSetFilters.Default.DataSetFilterControl1_FilterChanged(Object sender, EventArgs e) in c:\devlmnt\asp.net\datasetfilters\default.aspx.cs:130
Tompkins.Web.UI.WebControls.DataSetFilterControl.btnAddFilter_Click(Object o, EventArgs e) in c:\devlmnt\asp.net\tompkins.web\datasetfilter\datasetfiltercontrol.cs:739
Tompkins.Web.UI.WebControls.ButtonPlus._button_ServerClick(Object sender, EventArgs e) in c:\devlmnt\asp.net\tompkins.web\buttonplus\buttonplus.cs:272
System.Web.UI.HtmlControls.HtmlButton.OnServerClick(EventArgs e)
System.Web.UI.HtmlControls.HtmlButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain() +1277


# September 14, 2005 5:13 PM

Philip said:

Very nice blog!
# November 2, 2005 9:23 AM

Vu Nguyen said:

I can't believe it ! But i'm wondering why we not give our user ability to save their custom Filters set as a template for later using purpose ?

# October 11, 2006 4:26 AM

K.Jagan Mohan Reddy said:

I got what i want.

iam searching for months for ths.

thanks and Good job.

# March 10, 2007 2:54 AM

guestacc said:

hello, is it possible that you can share the source of this user control, i wanna add 'in' filter to this control. thanks.

# July 28, 2008 11:00 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

Free Tech Publications