DataView.Sort NaN

G

gilly3

I have a column in a DataView that contains NaN. When I attempt to Sort on
this column, I get:

MESSAGE: Index was outside the bounds of the array.
SOURCE: System.Data
STACKTRACE:
at System.Data.Index.Sort(Int32 left, Int32 right)
at System.Data.Index.InitRecords()
at System.Data.Index..ctor(DataTable table, Int32[] indexDesc,
DataViewRowState recordStates, IFilter rowFilter)
at System.Data.DataTable.GetIndex(Int32[] indexDesc, DataViewRowState
recordStates, IFilter rowFilter)
at System.Data.DataTable.GetIndex(String sort, DataViewRowState
recordStates, IFilter rowFilter)
at System.Data.DataView.UpdateIndex(Boolean force)
at System.Data.DataView.SetIndex(String newSort, DataViewRowState
newRowStates, DataFilter newRowFilter)
at System.Data.DataView.set_Sort(String value)

Here is my repro code:

public static void sortDataView()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("bar",Type.GetType("System.Double")));
dt.Columns.Add(new DataColumn("foo",Type.GetType("System.Double"),
"bar / 0"));
dt.Rows.Add(new Object[]{0});
dt.Rows.Add(new Object[]{0});
dt.DefaultView.Sort = "foo ASC"; // <-- This Line Errors!
}

However, if I debug with VS.NET 2003 and break just before the sort command
and run it in the Command Window, then continue, there is no error. I just
copied the line from the source that errors into the Command Window and it
executes just fine. I continue and my app runs without errors.

How do I fix this? (Besides fixing my divide by zero error)

thanks

-ivan.
 
D

DonGiaconia

Well I don't know if this helps too much, or if i'm just saying
something obvious, but NaN represents Not a Number, so it looks like it
is trying to sort with an invalid value in there.
 
I

INeedADip

isn't foo null....aren't you adding a (new Object[]{0}) which goes into
bar.
so that makes foo null and not a double.
 
G

gilly3

isn't foo null....aren't you adding a (new Object[]{0}) which goes into
bar.
so that makes foo null and not a double.

No, foo is a computed column:
new DataColumn("foo",Type.GetType("System.Double"),"bar / 0");

So foo contains bar / 0, which is, of course, always NaN.

The interesting thing is I can Sort a DataView with just one NaN value in
the sort column without error. If there are two or more NaN values in the
sort column, I get the error when I sort.

-ivan.
 

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

Similar Threads


Top