Does DataView(DataTable) constructor just return DataTable.DefaultView?

N

Neil Price

In developing a .NET website I've come across an issue with DataViews
that provoked some head scratching before ultimately finding a way
around it - but I'm curious as to the reason for the issue.

Using C# & Framework 1.1

1) Table in database (containing country codes & descriptions)

2) Function called within Application_Start selects all items into a
DataTable & stores the DataTable into ApplicationState

System.Web.HttpContext.Current.Application[countryDataTableKey] =
dataTable;

3) Static function on same object retrieves the data table, creates a
NEW view, and returns the view to the calling method.

DataTable dataTable =
(DataTable)System.Web.HttpContext.Current.Application[countryDataTableKey];
return new DataView(dataTable);

The reason for this being that I want the calling code to be able to
play with RowFilter, etc on their view without affecting other views
of the same data.


The first chunk of code databinds a DropDownList to the unchanged view
- worked fine, no problem.

All was well until I coded a separate page that actually changed the
RowFilter on the returned view while trying to match some text.

After performing this operation, the databound DropDownList was
finding no entries in the new DataView returned each time (in this
case there was no matching row from RowFilter set above). Before
performing this operation the DropDownList found all entries as
expected.

Given that these are supposedly two separate independant views, I
cannot see why this happens. However, changing the DataView
constructor to fully specify RowFilter, Sort, and DataViewRowState
solves the problem.

return new DataView(dataTable,"","Code",DataViewRowState.OriginalRows);


The only reason that I can conjure up for this is that the original
DataView(dataTable) call is actually returning a copy of the same
(modified) object, rather than a new instance.

Am I correct, or merely insufficiently well versed in the mysteries of
..NET?


Thanks,
Neil
 
M

Mat

VB.NET
Dim myDataview as Dataview= new Dataview(MytableName) ' This return very new
dataview without FILTER

To be sure that the dataview is without filter, before returning the view,
do this

mydataview.rowfilter=""
return mydataview.
 

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