linq to xml query .... sortability of datagridview

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hey,
feeling less like a newbie lately then I come across this challenge....
Im querying an XML database and sending to a DGV with a LINQ query. Thats all going fine. Now Id like the user to be able to use the inherent sorting function normally associated with the column header to sort the data.... the more I read it
looks like I need to bind the source to the DGV to sort it in this way... does this mean I need to send it to a dataset or a datatable? Need help with the concept more than anything.... code follows:
<pre class="prettyprint lang-vb Imports System.Xml
Imports System.IO


Public Class Form1
Public piepath As String = "C:UserspeteDesktopobject programmingxml data readingsorting xml data and sending to datagridXMLtestpies.xml"
Public doc As XDocument = XDocument.Load(piepath)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim query =
From el In doc.Descendants("RECIPE")
Where el.Element("NAME").Value = "MAMAS PIE"
From apple In el.Descendants("APPLE")
Order By apple.Element("NAME").Value
Select New With {
.NAME = apple.Element("NAME").Value,
.SWEETNESS = apple.Element("SWEETNESS").Value,
.COLOR = apple.Element("COLOR").Value
}
DataGridView1.DataSource = query.ToArray
End Sub
End Class[/code]
and the XML

<pre class="prettyprint <?xml version="1.0" encoding="utf-8"?>
<RECIPES>
<RECIPE>
<NAME>MAMAS PIE</NAME>
<STYLE>OLD FASHIONED</STYLE>
<CHEF>MAMA</CHEF>
<DATE>10</DATE>
<INGREDIENTS>
<APPLES>
<APPLE>
<NAME>Gala</NAME>
<SWEETNESS>4</SWEETNESS>
<COLOR>pink</COLOR>
</APPLE>
<APPLE>
<NAME>MACINTOSH</NAME>
<COLOR>RED</COLOR>
<SWEETNESS>1</SWEETNESS>
</APPLE>
<APPLE>
<NAME>GOLDEN DELICIOUS</NAME>
<COLOR>GOLD</COLOR>
<SWEETNESS>2</SWEETNESS>
</APPLE>
</APPLES>
<SUGARS>
<SUGAR>
<NAME>TRUCKLE</NAME>
<COLOR>BROWN</COLOR>
<PRICE>2.25</PRICE>
</SUGAR>
</SUGARS>
</INGREDIENTS>
</RECIPE>
<RECIPE>
<NAME>DADS PIE</NAME>
<STYLE>NEW AGE</STYLE>
<CHEF>DAD</CHEF>
<DATE>15</DATE>
<INGREDIENTS>
<APPLES>
<APPLE>
<NAME>GALA</NAME>
<COLOR>PINK</COLOR>
<SWEETNESS>3</SWEETNESS>
</APPLE>
<APPLE>
<NAME>GRANNY SMITH</NAME>
<COLOR>GOLD</COLOR>
<SWEETNESS>4</SWEETNESS>
</APPLE>
<APPLE>
<NAME>Golden Delicious</NAME>
<SWEETNESS>4</SWEETNESS>
<COLOR>gold</COLOR>
</APPLE>
</APPLES>
<SUGARS>
<SUGAR>
<NAME>CANE</NAME>
<COLOR>WHITE</COLOR>
<PRICE>1.5</PRICE>
</SUGAR>
</SUGARS>
</INGREDIENTS>
</RECIPE>
<RECIPE>
<NAME>PETES PIE</NAME>
<STYLE>OLD FASHIONED</STYLE>
<CHEF>MAMA</CHEF>
<DATE>12</DATE>
<INGREDIENTS>
<APPLES>
<APPLE>
<NAME>MACINTOSH</NAME>
<COLOR>RED</COLOR>
<SWEETNESS>1</SWEETNESS>
</APPLE>
<APPLE>
<NAME>GOLDEN DELICIOUS</NAME>
<COLOR>GOLD</COLOR>
<SWEETNESS>2</SWEETNESS>
</APPLE>
</APPLES>
<SUGARS>
<SUGAR>
<NAME>TRUCKLE</NAME>
<COLOR>BROWN</COLOR>
<PRICE>2.25</PRICE>
</SUGAR>
</SUGARS>
</INGREDIENTS>
</RECIPE>
</RECIPES>[/code]
Thanks yet again in advance

Pete

View the full article
 
Back
Top