Sort using XPath, XMLDataSource and a DataGridView?

246 views
Skip to first unread message

Darth Continent

unread,
Feb 5, 2008, 1:02:44 AM2/5/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
First some background, I have an XML file which looks like the
following:

<KW>
<Subjects SubjectID="00100" SubjectDescription="ABORTION"/>
<Subjects SubjectID="00010" SubjectDescription="ABUSE * ADULT
* INVESTIGATION"/>
<Subjects SubjectID="06450" SubjectDescription="ABUSE * CHILD
ABUSE * COUNSELING"/>
</KW>

Using the statement, below, I can set the XPath of my XMLDataSource,
which in turn filters the output displayed in the DataGridView I have
bound to the data source, based upon the text in a textbox, txtSearch.

MyXMLDataSource.XPath = "//KW/Subjects[contains(@SubjectDescription,'"
+ txtSearch.Text.Trim() + "')]";

For example, if txtSearch.Text = 'ABUSE', the last 2 of the above 3
records' values for the SubjectDescription attribute are returned.
This works like a champ.

Now I'd like to enable sorting for the result set returned by the
XPath query, but when I attempt a sort I get the message, "The data
source does not support sorting".

Based upon the above, could someone please help me enable sorting? I
first thought I could just use the DataGridView's own Sort method,
since the data has been brought into the DataGridView, but I guess
since it's still bound to the XMLDataSource, there lies the roadblock.

Any suggestions would be greatly appreciated!!

Cerebrus

unread,
Feb 5, 2008, 11:33:52 AM2/5/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Well, you are correct that the XmlDataSource does not support sorting
directly. But there are many ways to bind a DataGridView :

1. Sort your XML before loading it into the XmlDataSource.
2. Iterate through the nodes of the XML and get the relevant ones into
an Collection/ArrayList which can be sorted. Bind to this collection.
3. Convert the XML to a DataSet, get the default DataView and sort
that. Then bind your DGV to the DataView.
4. Use an XSLT stylesheet to transform the XML (in other words, sort
it) by using the TransformFile property of the XmlDataSource.
5...
6...
...

I would recommend Method 4 as the best method.
Reply all
Reply to author
Forward
0 new messages