ASP.Net DataTable Select Function to Filter Rows

Updated on 20 Aug 2012,
Published on 06 Sep 2008

DataTable Class of C# in ASP.Net provides a Select function that allows you to filter the selective rows based on the search criteria passed to the select function. You can perform the rows search exactly similar to the SQL select query with where clause, instead this method of ASP.Net DataTable Select function is easier. You just need to pass the column name along with conditional operator and value to filter the resulting rows based on specified search criteria. For example: "columnName = 10", "columnName <= 10", "columnName >= 10" or "columnName <> 10" for integer values, and "columnName = 'Tim'" for string values.

Get the C# code from the previous tutorial: ASP.Net DataColumn Expression Property to implement and understand the DataTable Select function to filter Rows.

C# Code for ASP.Net DataTable Select Function to Filter Rows

// DataTable.Select function returns the DataRow collection based on the filterExpression
DataRow[] filterRows = myDataTable.Select( "Unit_Price < 40", "" );

Add above C# code line below the code retrieved from the previous tutorial as mentioned above. It will filter the rows based on the search filterExpression passed to the Select function of DataTable.

ASP.Net DataTable Select function accepts two types of parameters:

  1. filterExpression: accepts the string type values as search string filter expression to filter the rows
  2. sort: accepts the string value as sort expression to sort the filtered rows based on the columnName and sort direction. 

ASP.Net DataTable Select function returns the collection of DataRows[ ].

Next step is how to display the filtered rows on the ASP.Net web page easily. Here we have used the HTMLTable to display the filtered rows data in a tabular form.

// initialize the instance of HTMLTable
HtmlTable tbl = new HtmlTable();
tbl.CellPadding = 5;
tbl.CellSpacing = 0;
tbl.Border = 1;
tbl.BorderColor = "#CCCCCC";

// Create HTMLTableRow Object
HtmlTableRow tblRow;

// create HTMLTableCell Object
HtmlTableCell tblCell;

// initialize the instance of HTMLTableRow 
tblRow = new HtmlTableRow();

// initialize the instance of HTMLTableCell
// th is passed as parameter to create the cell as table header cell
tblCell = new HtmlTableCell("th");
tblCell.Align = "left";
tblCell.InnerText = "Unit Name";

// add first header cell to the table row.

// initialize the second header cell for HTML table
tblCell = new HtmlTableCell("th");
tblCell.Align = "left";
tblCell.InnerText = "Unit Price";

// add second header cell to the table row.

// add row to the HTMLTable

// C# foreach loop over each filtered row item of DataRow collection
foreach (DataRow dRow in filterRows)
    // this code block creates 2 cells and adds them to the table row
    // then the new row is added to the HTMLTable
    // innerText property of HTMLTableCell is used here to display the DataColumn value in the HTML Table
    tblRow = new HtmlTableRow();
    tblCell = new HtmlTableCell();
    tblCell.InnerText = dRow["Unit_Name"].ToString();

    tblCell = new HtmlTableCell();
    tblCell.InnerText = dRow["Unit_Price"].ToString();


// add the HTMLTable to the Panel Control to render the HTMLTable code inside it dynamically

To display the HTMLTable, ASP.Net Panel control has been used:

<asp:Panel ID="Panel1" runat="server">

 Continue to next tutorial: ASP.Net C# DataTable Sort Rows using Select Function to learn how to sort the records in rows of a DataTable.

2 Responses to "ASP.Net DataTable Select Function to Filter Rows"
good,it's work....
It is good
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email