Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Filterable Grid for ASP.NET

0.00/5 (No votes)
12 Aug 2004 1  
An article on a filterable Grid for ASP.NET.

Sample Image

Introduction

This article presents an ASP.NET DataGrid with ability to filter based on column values.

Details

I tried to keep the code simple. This code uses JavaScript method filtergrid to build QueryString for the URL. The datasource is filtered based on QueryString values.

 // this method sets the filter condition.

function filtergrid(columnname)
{
  var val;
  var baseurl;
  
  // I have set the baseurl of the demo page here.

  // You can simply replace it with your .aspx page name

  // or may be read programmatically

  // to make this control generic.

  baseurl = 'FilterGridPage.aspx';
  val = document.getElementById('txt' + columnname).value;
  
  self.location.href = baseurl + '?selectedcolumn=' 
                   + columnname + '&selectedvalue=' + val;
}

In the code-behind file, the FillDataGridColumns method creates the column headers with HTML textbox and button.

   '// header cell code

   headerhtml = DisplayNames(i)
   
   '// create html text

   headerhtml = headerhtml & " <br> " & " & _
   "<Input type=text class=FilterTextBox id=txt" & ColumnNames(i) & " /> " & _
   "<Input class=ButtonStyle type=button id=btn" & ColumnNames(i) & _
   " onclick=""javascript:filtergrid('" & ColumnNames(i) & "');"" value='Go'/>"

   
   '// simply set the html text as HeaderText property of BoundColumn

   <BoundColumnVariable>.HeaderText = headerhtml

Scope of further development

  • At present, this control handles String and Double datatypes only. It can be modified to handle other datatypes.
  • Filter works on single value only and can not search for multiple values.

All your suggestions and comments are welcome.

Conclusion

The use of basic JavaScript and code-behind file helps to enhance the functionality of the DataGrid control.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here