DataTables and filters

M

Matt House

I have a DataTable that I have filled using a SqlDataReader (it holds
approximately 170 rows) used to populate a DataGrid inside of an ASP.NET
application. The DataGrid's footer contains a control to hold the sum of a
dollar amount of the displayed rows. At some point, a user may apply a
filter on the data to view only a subset of the data. I create a new
DataView, set the RowFilter property, and use that as the datasource for the
DataGrid. It seems the DataView knows how many rows meet the filter
criteria, but it is unable to display the correct rows. That is, for a
given filter, the DataView knows that there may be only N records, but it
always returns the first N from the unfiltered 170 row set.

How do I access the rows of a DataView that meet the RowFilter condition?
I've include my code below.

Thanks,

Matt

public void OnItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e) {

..

DataView dv = new DataView(dtInfo);

dv.RowFilter = (string)ViewState[this.ID + "wccDG_strGridFilter"];

dv.RowStateFilter = DataViewRowState.OriginalRows;

for (int i=0;i<dv.Count;i++) //dv.Count holds the correct
(filtered) value

{

if (dv.Table.Rows.IsNull(5) )

dFunding += 0.0M;

else

dFunding += Convert.ToDecimal(
dv.Table.Rows.ItemArray.GetValue(5) );

}

lblFunding.Text = dFunding.ToString("c0") + " Funding";

..
 
M

Marina

The problem is you are accessing the rows collection of the underlying
DataTable through the Table property. Well, this underlying datatable has
all 170 rows, and they are unfiltered of course - since the dataview is
doing all that.

You need to access the rows through the dataview's Item property.
 
M

Miha Markic

Hi Matt,

You should adderss DataView's rows instead of DataTable's.
if (dv[5] == null )
instead of
if (dv.Table.Rows.IsNull(5) )

Note: dv gives you a DataRowView which is similar to DataRow.
If you want to access the actual DataRow there is DataRowView.Row property.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top