DataGrid-DataView-XML-Sorting strings as numeric

J

Joe Rattz

I am populating a DataGrid with xml. I need to be able to
sort some of the columns. However, the DataView.Sort is
treating my strings (which are numbers in string form) as
strings and not formatting them as numbers. So, I get:

1,11,2,22

instead of:

1,2,11,22

How do I get the DataView.Sort to treat these strings as a
numeric type?

Thanks.
 
D

David Knipper

Here's a good way to do it...

First... You need to create an XML schema file along with your xml file.

Here's an example:

XML File:

<?xml version="1.0" encoding="utf-8" ?>
<Projects xmlns="projects.xsd">
<Project>
<ID>1</ID>
<Name>Project 01</Name>
<File>Test.txt</File>
<Integer_A>1</Integer_A>
<Integer_B>2</Integer_B>
<Status>Open</Status>
<Description></Description>
<Note></Note>
</Project>
</Projects>

Here's the XSD file (XML Schema):

<?xml version="1.0" ?>
<xs:schema id="Projects" targetNamespace="projects.xsd"
xmlns:mstns="projects.xsd" xmlns="projects.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified"
elementFormDefault="qualified">

<xs:element name="Projects" msdata:IsDataSet="true"
msdata:EnforceConstraints="False">

<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Project">
<xs:complexType>
<xs:sequence>
<xs:element name="S-ID" type="xs:integer"
minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="File" type="xs:string" minOccurs="0" />
<xs:element name="Posted_By_User" type="xs:integer" minOccurs="0"
/>
<xs:element name="Posted_For_User" type="xs:integer"
minOccurs="0" />
<xs:element name="Status" type="xs:string" minOccurs="0" />
<xs:element name="Description" minOccurs="0" />
<xs:element name="Date_Posted" type="xs:string" minOccurs="0" />
<xs:element name="Date_Received" type="xs:string" minOccurs="0"
/>
<xs:element name="Note" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
 
D

David Knipper

Here's a good way to do it...

First... You need to create an XML schema file along with your xml file.

Here's an example:

Original XML File (example):

<?xml version="1.0" encoding="utf-8" ?>
<Projects xmlns="projects.xsd">
<Project>
<ID>1</ID>
<Name>Project 01</Name>
<File>Test.txt</File>
<Integer_A>1</Integer_A>
<Integer_B>2</Integer_B>
<Status>Open</Status>
<Description></Description>
<Note></Note>
</Project>
</Projects>

Here's the XSD file (XML Schema):

<?xml version="1.0" ?>
<xs:schema id="Projects" targetNamespace="projects.xsd"
xmlns:mstns="projects.xsd" xmlns="projects.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
attributeFormDefault="qualified"
elementFormDefault="qualified">
<xs:element name="Projects" msdata:IsDataSet="true"
msdata:EnforceConstraints="False">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Project">
<xs:complexType>
<xs:sequence>
<xs:element name="S-ID" type="xs:integer"
minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="File" type="xs:string" minOccurs="0" />
<xs:element name="Integer_A" type="xs:integer"
minOccurs="0" />
<xs:element name="Integer_B" type="xs:integer"
minOccurs="0" />
<xs:element name="Status" type="xs:string" minOccurs="0" />
<xs:element name="Description" minOccurs="0"/>
<xs:element name="Note" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

Now when you use this XML file and schema in your datagrid... include
the schema in your dataset along with the xml file. Do it somewhat like
this...

//... c#

DataSet ds = new
DataSet(); ds.ReadXmlSchema(Server.MapPath("projects.xsd")); ds.ReadXml
(Server.MapPath("projects.xml"),XmlReadMode.InferSchema);
DataView dv = new DataView(ds.Tables[0]);

//...

DG.DataSource = dv;
DG.DataBind();

//...
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top