473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

dataview not sorting properly

I have a DataView derived from a Dataset object that gets its data from
an xml file. I am setting the Sort property of the Dataview object to
alphabetize the dataview. However, the resulting dataview is not sorted
but appears in the order of the original xml file. No exceptions are
thrown. Code below:

DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath("lib/leftlinks.xml"));
DataView dv=ds.Tables[0].DefaultView;
dv.Sort="linktitle desc";

I have done this with DB result sets but not with XML. Is there an issue
with XML? Thanks...!
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
3 7236
Phil,

How are you determining that the sort is out of order? Are you binding
to the dataview or to the table? If you are binding to the table, then it's
not going to do anything, since the default view doesn't affect how the
table is ordered. The default view is what is bound to when you present the
table as a data source (a data table is never bound to directly).

If anything, I wouldn't recommend this. I would create a new data view
and then set the sort order on that, and then bind to that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Phil Townsend" <ph*******@yahoo.com> wrote in message
news:O4**************@TK2MSFTNGP10.phx.gbl...
I have a DataView derived from a Dataset object that gets its data from
an xml file. I am setting the Sort property of the Dataview object to
alphabetize the dataview. However, the resulting dataview is not sorted
but appears in the order of the original xml file. No exceptions are
thrown. Code below:

DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath("lib/leftlinks.xml"));
DataView dv=ds.Tables[0].DefaultView;
dv.Sort="linktitle desc";

I have done this with DB result sets but not with XML. Is there an issue
with XML? Thanks...!
*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2
Here is the full code:

DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath("lib/leftlinks.xml"));
DataView dv=ds.Tables[0].DefaultView;
dv.Sort="linktitle desc";

foreach(DataRow dr in dv.Table.Rows)
{
TableRow row=new TableRow();
TableCell cell=new TableCell();
cell.Text=String.Format("<a href=\"{0}\"><p
class=\"leftlinks\">{1}</p></a>",
(string)dr["url"],(string)dr["linktitle"]);
row.Cells.Add(cell);
row.Attributes.Add("onMouseOver","highlight(this)" );
row.Attributes.Add("onMouseOut","unhighlight(this) ");
tblLeftLinks.Rows.Add(row);

TableRow rowsep=new TableRow();
TableCell sep=new TableCell();
sep.Text="<img src=\"images/lnkBevel.gif\" width=\"135\"
height=\"2\">";
rowsep.Cells.Add(sep);
tblLeftLinks.Rows.Add(rowsep);
}

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Phil,

That's the problem. While you are using a dataview, you are not
enumerating through the rows on the dataview. You are referencing the table
that the view is based on, and then cycling through the rows of the original
table (which are not ordered).

You want to change your loop code to this:

foreach (DataRowView dr in dv)

This will enumerate through the rows as the view has filtered/ordered
them by.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Phil Townsend" <ph*******@yahoo.com> wrote in message
news:uy**************@TK2MSFTNGP10.phx.gbl...
Here is the full code:

DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath("lib/leftlinks.xml"));
DataView dv=ds.Tables[0].DefaultView;
dv.Sort="linktitle desc";

foreach(DataRow dr in dv.Table.Rows)
{
TableRow row=new TableRow();
TableCell cell=new TableCell();
cell.Text=String.Format("<a href=\"{0}\"><p
class=\"leftlinks\">{1}</p></a>",
(string)dr["url"],(string)dr["linktitle"]);
row.Cells.Add(cell);
row.Attributes.Add("onMouseOver","highlight(this)" );
row.Attributes.Add("onMouseOut","unhighlight(this) ");
tblLeftLinks.Rows.Add(row);

TableRow rowsep=new TableRow();
TableCell sep=new TableCell();
sep.Text="<img src=\"images/lnkBevel.gif\" width=\"135\"
height=\"2\">";
rowsep.Cells.Add(sep);
tblLeftLinks.Rows.Add(rowsep);
}

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
9
by: Raymond Lewallen | last post by:
I have a dataview in which the sort property will not sort the dataview. Here's is a simple scenario similar to what I am doing: Class Foo Private Function Retrieve() As DataView ' Returns a...
1
by: TaeHo Yoo | last post by:
Hi all, After sorting and grouping data using a dataview, then how to transfer the changed datatable in the dataview to a datatable in C#? Cheers
0
by: grolms | last post by:
I must recreated dataview . I am in the designer insert dataview set datasource ... And I set before DataBind datasource on the dataview ... How I recreated dataview. I have dataview in class...
4
by: Aaron Smith | last post by:
Dim dv As DataView = New DataView(FacilitiesDS1.Facilities, "", "ID ASC", DataViewRowState.CurrentRows) Dim iPos As Integer = dv.Find(dr.Item("ID")) Me.BindingContext(FacilitiesDS1,...
0
by: Nathan Franklin | last post by:
Hello Guys, I have been trying to work this our for so long, but I just can't seem to find the answer. I am loading a datatable from a an access database using an oledbdataadapter. I then...
6
by: Nick | last post by:
I have a code that returns data in IList. My webGrid doesn't allow me to sort with IList returned, it say it only suports DataView, DataTable and DataSet, not IEnumerable. I don't know how to...
0
by: Pravin Pujari | last post by:
Hi All, I am using .net framework 1.1 and c#. I have one problem regarding dataview. I have one dataview to which sorting may be applied or may not be applied. I have one UI component where I...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.