DataGrid-DataView-XML-Sorting strings as numeric

Discussion in 'ASP .Net Datagrid Control' started by Joe Rattz, Nov 19, 2003.

  1. Joe Rattz

    Joe Rattz Guest

    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.
    Joe Rattz, Nov 19, 2003
    #1
    1. Advertising

  2. 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>



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    David Knipper, Nov 20, 2003
    #2
    1. Advertising

  3. 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();

    //...



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    David Knipper, Nov 20, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders

    Re: alpha numeric sorting for dataview

    John Saunders, Jul 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,694
    John Saunders
    Jul 20, 2004
  2. CodeMonkey
    Replies:
    1
    Views:
    728
    joyal jhaveri
    Feb 4, 2011
  3. Arne Vajhøj

    Re: Sorting numeric strings

    Arne Vajhøj, May 1, 2012, in forum: Java
    Replies:
    0
    Views:
    380
    Arne Vajhøj
    May 1, 2012
  4. Gene Wirchenko

    Re: Sorting numeric strings

    Gene Wirchenko, May 1, 2012, in forum: Java
    Replies:
    0
    Views:
    388
    Gene Wirchenko
    May 1, 2012
  5. Daniel Pitts

    Re: Sorting numeric strings

    Daniel Pitts, May 1, 2012, in forum: Java
    Replies:
    14
    Views:
    842
    Gene Wirchenko
    May 7, 2012
Loading...

Share This Page