C# - XPath, XMLDataSource and a DataGridView

  • Thread starter Darth Continent
  • Start date
D

Darth Continent

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!!
 
M

Martin Honnen

Darth said:
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?

You could do the sorting and the filtering using an XSLT stylesheet as
the XmlDataSource allows applying an XSLT transformation.

But you need to tell us what you want to sort on.
 
D

Darth Continent

Let's say for example I wanted to be able to sort by the
SubjectDescription field in this example? How then could I do what
you describe?
 
M

Marc Gravell

With xslt? within an <xsl:for-each/> or <xsl:apply-templates/> (on the
Subjects), have <xsl:sort select="SubjectDescription"/>; but first you
need to understand xslt...

Marc
 
M

Martin Honnen

Darth said:
Let's say for example I wanted to be able to sort by the
SubjectDescription field in this example? How then could I do what
you describe?

Well with the sample you posted earlier there is an attribute named
SubjectDescription.
Here is a sample stylesheet

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:param name="subject"/>

<xsl:template match="KW">
<xsl:copy>
<xsl:apply-templates
select="Subjects[contains(@SubjectDescription, $subject)]">
<xsl:sort select="@SubjectDescription"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

ASP.NET code would look like
<asp:XmlDataSource runat="server" ID="ds1"
TransformFile="XSLTFile.xsl" DataFile="XMLFile.xml" XPath="KW/Subjects" />


The stylesheet defines a parameter named 'subject' later used as
$subject in the stylesheet. You can set parameters on the transformation
applied by the XmlDataSource control from code, e.g. assuming ds1 is an
XmlDataSource control you can do

XsltArgumentList args = new XsltArgumentList();
args.AddParam("subject", "", "ABUSE");
ds1.TransformArgumentList = args;

If you need more help on XSLT then I think microsoft.public.xsl is a
better place to discuss that than this C# group.
 

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