I’m trying to figure how to correctly return a dataview. I noticed that if I bind to the resultant dataview used below, I don’t get any results.
private DataView filterDocuments(DataSet documents, string classType)
{
using (documents)
{
//filter it
using (DataView documentsByType = new DataView(documents.Tables[0], String.Format("ClassName = '{0}'", classType), String.Empty, DataViewRowState.CurrentRows))
{
return documentsByType;
}
}
}
However, if I don’t dispose of my dataview, I can see teh results I want. How can I properly return a dataview?
private DataView filterDocuments(DataSet documents, string classType)
{
using (documents)
{
//filter it
return new DataView(documents.Tables[0], String.Format("ClassName = '{0}'",classType), String.Empty, DataViewRowState.CurrentRows);
}
}
Do I not need to dispose of it? Am I creating a memory leak? Do I have to pass the dataset by reference?
Many thanks!
The correct place for the
using(or callingDataView.Dispose()would be wherever you are making use of theDataView. It doesn’t make sense to dispose of it in this spot.