System.Data Namespace DataView Class
Sets or retrieves the expression used to filter which rows are viewed in the DataView.
Script |
DataView.RowFilter [ = strExpression ] |
strExpression |
A string that specifies how rows are to be filtered. |
The property is read/write with no default value.
To form a RowFilter value, specify the name of a column followed by an operator and a value to filter on. The value must be set in quotes. For example:
myView.RowFilter = "LastName = 'Smith'"
See the DataColumn class's Expression property for more information.
The following examples demonstrate using the RowFilter property to programmatically "filter" the records in a DataView at run time, based on user input.
The first example shows how to dynamically retrieve a set of records from a DataTable, given a RowFilter based on user input.
DataView myView = ( ( DataTable ) Session [ "tblGear" ] ).DefaultView;
myView.RowFilter = "Description LIKE '%" + strSearch.Text +
"%' or Model LIKE '%" + strSearch.Text + "%' ";
Dim myView As DataView = CType ( Session ( "tblGear" ), DataTable ).DefaultView
myView.RowFilter = "Description LIKE '%" + strSearch.Text + _
"%' or Model LIKE '%" + strSearch.Text + "%' " |
|
C# |
VB |
The second example is basically the same, except that the result sets are rendered into a DataGrid, with paging controls automatically enabled when the record count is more than the specified page size.
int found = myView.Count;
...
myGrid.Visible = ( found > 0 );
myGrid.PagerStyle.Visible = ( found > myGrid.PageSize );
Dim found As Integer = myView.Count
...
myGrid.Visible = found > 0
myGrid.PagerStyle.Visible = found > myGrid.PageSize |
|
C# |
VB |
The last example shows how to use RowFilter to return a single record. tblSource.DefaultView
returns the reference to the DataView, the RowFilter value of which is set using a column value ( PlanId ) from a dynamically specified row.
DataView getView ( int rowIndex ) {
DataTable tblSource = ( DataTable ) Session [ "tblSource" ];
tblSource.DefaultView.RowFilter = "PlanID='" + tblSource.Rows [ rowIndex-1 ] [ "PlanId" ] + "'";
return tblSource.DefaultView;
}
Function getView ( rowIndex As Integer ) As DataView
Dim tblSource As DataTable = CType ( Session ( "tblSource" ), DataTable )
tblSource.DefaultView.RowFilter = "PlanID='" + tblSource.Rows ( rowIndex - 1 ) ( "PlanId" ) + "'"
Return tblSource.DefaultView
End Function |
|
C# |
VB |
DataView Members Expression Sort