dataview sort problem

J

Jim

I have a dataview that I'm trying to apply a sort to, but when I
iterate through the rows in the dataview, they are still in the
original order. Here's my code. I'm using an XML document to populate
my DataSet

DataView dvProject = new DataView(MyDS.Tables["Project"], "FY = 2008",
"FY ASC", DataViewRowState.None);
// dvProject.Sort = "FY ASC, Quarter ASC";
Console.WriteLine("{0}", dvProject.Sort);

foreach (DataRow _Project in dvProject.Table.Rows)
{
Console.WriteLine("{0}{1}", _Project["FY"], _Project["Quarter"]);
}

Should be fairly straight-forward right?? I even tried to put on a
rowfilter with no luck. Anyone have any ideas? thx --jim
 
M

Marina Levit [MVP]

The rows never physically get sorted. The view provides a sorted view of the
data. The table never changes. You have to access the data through the view
to see it sorted. Going to the table itself does nothing - no reason to
create the view in the first place.

This is why you can have multiple views on one table. Otherwise, which one
would win?
 
J

Jim Hughes

by using the Table property, you are going "up" a level and ignoring the
DataView completely.

foreach (DataRow _Project in dvProject.Table.Rows)

Instead of using foreach, use a for loop since the order "matters" to you
since you are using a dataview.
DataTable dt = new DataTable();

DataView db = new DataView(dt);

for (int i=0;i<dv.Count;i++)

{

dv.

}
 
G

Guest

Jim,

Sorting a dataview does not change the order of the rows in the dataview's
underlying table.

You might want to get each datarowview from the dataview. They will be in
sorted order.

Kerry Moorman
 
J

Jim

Thank everyone. I didn't realize using the .Table pointed me back to
my original table. I've got it working now. Much appreciated!!

Jim

Jim said:
by using the Table property, you are going "up" a level and ignoring the
DataView completely.

foreach (DataRow _Project in dvProject.Table.Rows)

Instead of using foreach, use a for loop since the order "matters" to you
since you are using a dataview.
DataTable dt = new DataTable();

DataView db = new DataView(dt);

for (int i=0;i<dv.Count;i++)

{

dv.

}


Jim said:
I have a dataview that I'm trying to apply a sort to, but when I
iterate through the rows in the dataview, they are still in the
original order. Here's my code. I'm using an XML document to populate
my DataSet

DataView dvProject = new DataView(MyDS.Tables["Project"], "FY = 2008",
"FY ASC", DataViewRowState.None);
// dvProject.Sort = "FY ASC, Quarter ASC";
Console.WriteLine("{0}", dvProject.Sort);

foreach (DataRow _Project in dvProject.Table.Rows)
{
Console.WriteLine("{0}{1}", _Project["FY"], _Project["Quarter"]);
}

Should be fairly straight-forward right?? I even tried to put on a
rowfilter with no luck. Anyone have any ideas? thx --jim
 

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