Ross Coded Classes

May 10, 2007

DataView RowFilter Performance

Filed under: .NET Tips — Tags: , — Ross Bradbury @ 9:45 pm
Avoid using the default System.Data.DataView constructor followed by setting the RowFilter property.  When you want to create a System.Data.DataView object and apply a RowFilter, use the DataView constructor that takes the rowFilter parameter.  This eliminates processesing of DataRowView objects that are later removed when the RowFilter property is set.  This is particularly apparent when the DataTable has many rows and the RowFilter eliminates most of them.
 
Note that you will need to specify Sort and DataRowViewState options to use this constructor.  The defaults are an empty string for Sort and DataViewRowState.CurrentRows for RowState.
DataView slowWay = new DataView(table);
slowWay.RowFilter = rowFilter;

DataView fastWay = new DataView(table,
                                rowFilter,
                                String.Empty,
                                DataViewRowState.CurrentRows);

Blog at WordPress.com.